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, embarrassing 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.

I come before you with a question, a yearning in my heart. What <dramatic pause> is null..... Equal to?

...
if (value == null || value.equals(null)) {
return"null";
}
...

Evidently, the concept of "null" and "null pointers" and "null pointer calls" is lost on the guys who wrote this particular line of code. It doesn't make any errors, probably because someone added the first condition after a few NPEs happened...

This, would execute if there is no value in i, or the i was never initialized. The code that you're having is something like this

// if the variable of value is not initialized// or contains nothing, does not exist in memory// .equals(null) is a string method, to check string value// and has same functionalityif (value == null || value.equals(null)) {
// then return a string that is NOT null but contains "null"return"null";
}

The method signature would be like,

publicstring Function1 () {
// returns a string
}

Favourite line: Throw me to them wolves and close the gate up. I am afraid of what will happen to them wolves - Eminem
~! Firewall !~

Mmmh, yeah, there's stuff like that in there too. I'll have to see if I can anonymize it enough to be internet postable. Let's just say someone did not understand object oriented coding and how Java works. We got to use this library before we actually took over developing it as contractors, and all the problems we had with it before are starting to make sense now...

The problem is not the result of "null", the problem is the value.equals(null).
The first part of the if is right (value == null). The second is wrong and it would actually never execute when the value is null (and when it does execute, value will not be null, avoiding an exception).

If it was VB, yes, but not in C#. If it is null, the second part will not be evaluated. It's effectively a dead condition. So, it won't blow up - thanks to this oversight.

I vaguely remember a manager who claimed that we always should use ".equals" for comparisons, and that it was a best practice. Using the operator is not only more readable, it also does not depend on the object having a value.

“==” compares if the object references are same while “.Equals()” compares if the contents are same.
So if you run the below code both “==” and “.Equals()” returns true because content as well as references are same.