Click here to Skip to main content

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrasing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralHow do you like your nullsmemberRobert Hoffmann8-Dec-12 1:57 
In a project i had to refactor:
 
if (!string.isNullOrEmpty(User.ToString())) {
 
}
 

null.ToString() ?? sweet Big Grin | :-D
GeneralRe: How do you like your nulls PinmemberZac Greve8-Dec-12 7:25 
D'Oh! | :doh: Laugh | :laugh:

Bob Dole
The internet is a great way to get on the net.

D'Oh! | :doh: 2.0.82.7292 SP6a

GeneralRe: How do you like your nulls Pinmemberlewax008-Dec-12 15:32 
I'm not sure I get what's wrong here with the given amount of context (except that ToString probably shouldn't return null...but it certainly could if it's overloaded) D'Oh! | :doh:
GeneralRe: How do you like your nulls PinmemberZac Greve8-Dec-12 16:23 
ToString is an instance method, and if the user variable is null, there will be an unhandled NullReferenceException when the ToString() is called, as you cannot call a method on a null object.

Bob Dole
The internet is a great way to get on the net.

D'Oh! | :doh: 2.0.82.7292 SP6a

GeneralRe: How do you like your nulls Pinmemberlewax008-Dec-12 16:33 
Right, but there's no reason to expect it might be null at this piece of code without more context. The previous line could be something that gets that user from a function that always returns an non-null object...I just don't think there's enough information to comment on the code as-is.
AnswerRe: How do you like your nulls PinmemberRobert Hoffmann8-Dec-12 23:55 
I understand your logic, but User can be null.
 
But either way turned ToString in this situation is also completely redundant because IsNullOrEmpty already does 2 specific checks. Is the item null, or is the item an empty string.
 
So the correct code, without triggering a NullReferenceException should have been
if (!string.IsNullOrEmpty(User)) {
 
}
 
Thumbs Up | :thumbsup:
GeneralRe: How do you like your nulls Pinmemberlewax009-Dec-12 0:35 
Oh is User a string? If so I get it now...
GeneralRe: How do you like your nulls PinmemberRobert Hoffmann9-Dec-12 9:39 
Yeah it's a string and probably read UserName originally. Sorry for the confusion Shucks | :->
GeneralRe: How do you like your nulls Pinmemberpatbob10-Dec-12 5:32 
Calling ToString() on a string is totally unexpected. Perfectly valid code, but I'd wonder about the programmer who wrote it.
 
How that line ever got written in the first place made a lot more sense when I was thinking User wasn't a string -- you can't pass a non-string to IsNullOrEmpty(). Still broke, but I can see someone making such a mistake if in a hurry. Any chance it wasn't a string at one time?
We can program with only 1's, but if all you've got are zeros, you've got nothing.

GeneralRe: How do you like your nulls PinmemberTomz_KV10-Dec-12 2:28 
You are correct. That is the existing implementation (or null handling). In my opinion, it would be nice to allow the conversion (.ToString()) from null to an empty string. When something is nothing (null), it is logical to think it is an empty in terms of a string. However, this ertainly not applicable to other data types.
TOMZ_KV

GeneralRe: How do you like your nulls PinmemberZac Greve10-Dec-12 9:17 
True. That would be useful.

Bob Dole
The internet is a great way to get on the net.

D'Oh! | :doh: 2.0.82.7292 SP6a

GeneralRe: How do you like your nulls PinmemberEddy Vluggen10-Dec-12 14:26 
Zac Greve wrote:
ToString is an instance method

Replace it with an extension-method Smile | :)
Bastard Programmer from Hell Suspicious | :suss:
If you can't read my code, try converting it here[^]
They hate us for our freedom![^]

GeneralRe: How do you like your nulls PinmemberBobJanova9-Dec-12 22:49 
Returning null from a ToString would be worthy of a WTF of its own.
GeneralRe: How do you like your nulls PinmemberRafagaX10-Dec-12 5:28 
i prefer:
 
 if(User != null && User.ToString() != "")
 
Poke tongue | ;-P
CEO at:
- Rafaga Systems
- Para Facturas
- Modern Components
for the moment...

GeneralRe: How do you like your nulls Pinmemberjibesh12-Dec-12 22:35 
how about this.
 
if( User != null && User.Length != 0) is better than checking for empty string ??? Roll eyes | :rolleyes:
Jibesh.V.P
India

GeneralRe: How do you like your nulls PinmemberRafagaX13-Dec-12 4:30 
D'Oh! | :doh:
 
Why didn't i though it?
CEO at:
- Rafaga Systems
- Para Facturas
- Modern Components
for the moment...

GeneralRe: How do you like your nulls PinprotectorAspDotNetDev10-Dec-12 6:35 
Maybe they have CDO?
 
if (!string.IsNullOrEmpty(User.ToString().ToString().ToString().ToString())) {
  // OK, now I can be sure it's a string. It's a string. A string. String.
}

JokeRe: How do you like your nulls PinmemberZac Greve10-Dec-12 9:18 
CDO. It's like OCD, just in alphabetical order like it should be!

Bob Dole
The internet is a great way to get on the net.

D'Oh! | :doh: 2.0.82.7292 SP6a

GeneralRe: How do you like your nulls PinmemberFlorin Jurcovici11-Dec-12 10:18 
I like my nulls to be null objects.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   


Advertise | Privacy | Mobile
Web01 | 2.6.130617.1 | Last Updated 19 Jun 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid