- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Anyone else noticed that the forums (incl. Sidebar) are broken?
Admin
A) Some people should be legally barred from coding B) re: sidebar: yeah, the links are still pointing at the now defunct worsethanfailure.com
Admin
I actually had to read that second one three times before I believed that someone could be that unbelievably stupid!!
Admin
The second one was from Northern Rock
Admin
The first one made me laugh. The second one frightened me!
Admin
The first one is a bone-headed programmer WTF (Java's supposed to work out the class of the 'null' exactly how?) but the second is more interesting. It's possibly not a WTF of the programmer of that code; it could be that the Warning class has two overloaded methods where the first argument can be one of several types. On the other hand, that then makes the Warning class itself the WTF if it treats 'null' differently according to which route it came through. I can totally believe that being the case...
Admin
I have come across LOTS of code in my project that follows the I-Hope-I'm-Not-Null pattern:
Admin
AHHHHHHHH MY EYES!!!!!!!!
Admin
Hmm... the bigger issue, besides the shit code, is that the first one was written by a "senior developer". I would imagine a senior developer to know better, but then again this IS the Daily WTF, and most of the snippets/evil stuff here was written by "senior" developers.
Admin
OK, you could override equals() so that returns true in some cases. But why would you?
Admin
I'm not sure if I understand the WTFiness of the first one, since I'm not versed in how Java treats nulls. Is the WTF that Java would already throw an exception on it's own if you access a null?
Admin
Apologies for posting a JPEG of a book's text, but I think that this excerpt is relevant.
Admin
The system I'm developing is written in a language that implicitly returns null in case nothing is returned explicitly. Thus, it makes perfect sense to use following pattern on a regular basis, because otherwise an implicit return null (caution: very dangerous) would happen without warning:
The code was riddled with variations of such a construct. There was an even more explicit version:
It took a while to eliminate every single instance of such nonsense with this functionally equivalent construct:
Admin
Is this a Java thing? I get you'd never design a system this way yourself, but what if they are interacting with a third party library in C++ that returns pointers? One would expect that you'd check to see that a pointer returned isn't NULL before it's dereferenced.
If the fact that the pointer is NULL is a failing case anyway, then failIfNull() collapses four physical lines of code down to one, and eliminates a layer of indirection. If you're making a dozen calls in a row into this library then failIfNull() eliminates a lot of redundant code.
Admin
Admin
Mostly correct. o.getClass() will throw a null pointer exception b/c 'o' is null.
Admin
Admin
One of the best parts of the article got completely ignore by all commenters!
Warning.showWarning((JFrame) null, title, msg);
He's casting null into a JFrame! It really makes you wonder what that would do if it would actually work ... create a special null that isn't actually null but an empty JFrame ?
Admin
The first one is a WTF because Java will throw a subclass of RuntimeException called NullPointerException if an attempt to dereference a null is made. The code attempts to dereference a null, having already established it's null, which results in a NullPointerException anyway - not the plain RuntimeException that the programmer tried to construct.
As for the second one, it's utterly redundant. You cannot call a method on yourself until you're constructed. Even calling a method on itself from the objects constructor wont result in "this" being null.
Admin
So, strangely enough, I've seen places where that was needed.
In a C++ DLL I was working on there was this weird race condition happening during the unloading of the DLL, and one of the methods would occasionally crash. When walking through it with the debugger I found that the crash happened because
this
was equal to NULL. I never managed to find the root cause of the problem, but just putting`if(this == NULL) { return; }
at the top of the function fixed the crash. Since it was happening when the DLL was being unloaded and the results of that function weren't really important anymore we decided it wasn't worth killing ourselves trying to find the root cause anymore.
Fortunately, my current project is to rewrite that entire DLL (mainly triggered by Vista incompatibilities. Thanks Vista!), so hopefully I'll be able to avoid that this time. :)
Admin
I don't know enough about 'showWarning', but in the Windows SDK, 'MessageBox' is passed a window handle as the first parameter if you want the MessageBox to be the child of another window, or you pass 'NULL' if you want it not to be a child window of anything.
I'd guess that it's possibly the same here.
Admin
Yes, both snippets invoke methods on null objects. You cannot invoke a method in a null object, java throws a NullPointerException :)
Admin
If the Warning class is modelled on JOptionPane, then the cast is probably ensuring a call to showWarning(JFrame f, ...) rather than showWarning(JDialog d, ...). Without the cast it wouldn't even compile, as the compiler hasn't enough information to determine which method to call.
Admin
It looks like it's the DNS entry for 'forums.worsethanfailure.com' which got lost sometime yesterday, and then got re-instated. But the negative TTL is 1 day on the worsethanfailure.com domain, so your DNS server is remembering that 'forums.' doesn't exist, even though it now does.
Add 74.50.106.245 forums.worsethanfailure.com to your hosts file (or wait a day for the negative TTL to expire)
Admin
Admin
Actually, I've found doing some quick checks for null at the top of a function and explicitly throwing an exception if they're null is a good thing. While it's true that Java will throw a NullPointerException when dereferencing a null, by doing the check at the beginning of the function and throwing there, you prevent the possibility of getting partway through your processing (and possibly changing some class state) and then causing the exception. Failing fast is almost always better.
That said, the check done in that first one is totally broken anyway, as it's dereferencing the null when trying to generate the exception.
Admin
It's probably to remove ambiguity. Warning.showWarning probably has multiple signatures, with the only difference being the type of the first argument. When you pass 'null' explicitly (as opposed to a variable that's set to null) the compiler doesn't know the type, so it can't determine which version of the method to call. By casting it to a JFrame the compiler knows which showWarning() method to call.
Admin
Ah, but it ain't necessarily so (at least in C++)
#include <stdio.h>
class myObject { public: void Print(void) { printf("0x%08lx\n", this); } };
myObject *foo() { return NULL; }
int main() { myObject *p = foo(); p->Print(); }
Here the 'Print' object will be called on an object whose 'this' pointer is NULL, so it'll print out '0x00000000'
Admin
Yeah, I was already reminded about that on IRC. I guess today I'm the real WTF (tm).
Admin
You know what happens when you abuse something, it gets taken away! No more null for you!
Admin
Admin
That code raises an interesting paradox (if that's the right word), because the code would be executing within the scope of that object, so if it were to be null then that code wouldn't exist, so it's checking if itself exists, but if it didn't exist, there would be nothing being checked...ARRHHH!! MY HEAD!!!!!!!!!
Admin
OOPS, I take that back. That actually does work.
Admin
Ugh. This is the first time in a while I've actually placed my head into my hands and groaned after looking at code.
Admin
The fact that is this works in C++ is today's Real WTF?!
Admin
On the project I'm working on we have a design constraint of "no Null Pointer Exceptions... ever". So we understand well the value of nulls... (code summary, not actual code)
PS: that was sarcasm. still, I'd rather have this than the article's code
Admin
This works in C++ because a non-virtual member function is effectively just a C function with a mangled name and a hidden parameter passed in (the this pointer). Thus it won't know it's being called on a null object until it tries to dereference this. In the virtual case it immediately has to do so in order to look up the function in the vtable, thus immediate segfault. Not really a WTF, just an implementation detail imo.
Admin
That's not the problem, the problem is the "o.getClass().getName()" snippet. If "o" is Null, then you'll get a NullPointerException thrown, and you'll never get any value for the class name.
Luckily for this joker, a NullPointerException is a subtype of RuntimeException, so everything still works. If he had set it to throw some other kind of exception, he'd be in trouble, as the NullPointerException would crash the whole thing. He'll also never know what class happened to be null.
~Sticky
Admin
Admin
Admin
Isn't the wtf that in the block of code that only runs if o is null, he's USING o, and attempting to access a property of o ???
Admin
I think you are missing the point... this.equals(null) implies that this is not null. Had they had remotely a condition like what you describe the code would have been:
// This check is because of a race condition that occurres when XYZ happens. if(this == null) { // handle }
{notice the comment :) )
Admin
Typecast of null is mandatory :)
Admin
Senior doesn't necessarily mean experienced or knowledgable.
Admin
Ever heard about method overloading. There might be 2 show warning methods with 3 params.
showWarning(JFrame frame, String title, String msg); Warning.showWarning(JDialog dialog, String title, String msg);
The cast is needed to specify which of both methods needs to be invoked.
Admin
No, the WTF is that in Java you don't have to declare a RuntimeException.
Admin
The failIfNull-type function can actually be quite useful in C++. Because throwing an exception generates a boatload of code, you can considerably reduce your code size by replacing a dozen similar throws with a call to a function that throws.
Admin
Even if the second snippet wasn't dereferencing a null reference, it's still useless. All he'd get was the name of the class the reference points to, not the name of the variable.
Prints out:
Awesome! java.lang.String is null! That's real helpful. He'll still have to follow the stack trace to find out which variable is null.
Admin
Admin
And that's the real WTF of the first one. If the object is null, how can you expect to run a method on it?