- 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
I'm still a little uncertain as to the purpose of the function; it's name and input implies that it is to test whether the given input which could be anything is an empty string. Thus, it would appear to be a rather simple routine: check whether the input is a string, and then check whether the string is empty, returning false should either check fail.
Without the function's exact specification, however, there are other possibilities for interpretation. It could simply be a check whether the supplied string was empty. In which case, the function defintion should be tightened up accordingly, identifying the supplied parameter as being a string type, rather than a generic object.
My suggestion for concise rewriting of the function according to my first interpretation would be:
Commentary on other commentary
The documentation for .NET's IsNullOrEmpty suggests that it will only accept an object of type String as an input, undoubtedly resulting in a compile time type error, or a runtime cast error if any non string type is supplied. Additionally it would returning True for null, which would, in my opinion, be an invalid result (as the function name is IsEmptyString, not IsEmptyOrNullString).
It is my opinion that suggesting that the function (and, therefore, the rest of the system) be written in a different language is just ludicrous. We have no idea how large the system is and, therefore, the implied cost of the suggestion.
Furthermore, renaming the function to represent it's action is equally silly. There is an indicated requirement for a function called IsEmptyString that performs an appropriate computation, and no indication is present that any variant, or inversion thereof, is required.
Admin
Ooh, a new, more efficient wheel?
Admin
"From the comment it looks like he wants to return true if there's a string of whitespace so I'd say isEmpty is the one he wants. Also checking to see that the object is a String at all. Other than that I agree this is totally reinventing the wheel, but reinventing it with corners."
Ooh, a new, more efficient wheel?
Admin
It's threads like this that reaffirm my conviction that Java really, really sucks.
Admin
Because people who don't know about Java are making incorrect statements and assumptions about Java, is sucks?
Or do you have a valid reason for thinking that Java sucks?
Admin
Barring Ada, Java is pretty much at the top of my list for coder-unfriendly languages.
Admin
I have no idea what this code is trying to do; absolutely none. Please fix the code below to taste.
Now.. What is an appropriate name?
Admin
About half of the debate has been about what the function is supposed to accomplish regardless of language, the other half seems split between how best to accomplish it in Java, and how best to accomplish it in C#. Both languages have a single static method that would accomplish the given task, how then is Java less coder-friendly than C#?
Admin
Alex, Derrick, et al.,
Could you guys perhaps mark these code snippets with the language in which they're written, either with an explicit tag or in some obvious fashion in the text of the article? It's amusing at first to see all the .NET fixes being posted for Java code, but it gets old pretty quickly...
Admin
Mind you, the real WTF is when some commenter replies when (a) he assumes this guy is an experienced dev. who the f knows? (b)assumes the type of company it is and what kind of depts they have in place for testing, the size of the company, or that they would have time for unit testing, or even have such methodologies in place. (c) that there is something wrong with asking him to rewrite it just cause he goofed once.. my god man, wtf are you, the godsend of devs? (d) see (b) avove.
The fact that the second version of the code makes you head spin means you are someone I would dread to work with. How unhelpful, spiteful, and ego-driven.
You should have added (e)
The real WTF are companies that hire pretentious asses who make your work life a living hell, and have no ppl skills whatsoever.
Admin
I wonder why the same guy had to fix it. I believe this function was a result of a bigger design mistake, the whole function looks crazy, so probably someone should explain the new guy what he did wrong. "no good, fix it" is usualy not enough.
Admin
I think the WTF is that they did explain to him what he did wrong, and he still didn't get it.
Admin
In order to explain a function well enough that someone else could do exactly what you want, you pretty much end up doing all the work yourself. This is why software development is expensive... a robust design phase is not an optional accessory. It's when you try to save money and say things like "just do it" that you get code which asks back: "just do what?"
Admin
The real WTF is the common perception that unit testing wastes time rather than saving it. A software project that's in trouble "doesn't have time for unit testing". In the same way that person facing a tiger "doesn't have time buy a gun".
Admin
Great analogy, so if you ever encountered a tiger I assume you would immediately stop and take the time to buy a gun rather than simply running like hell. That is why bad things happen to good projects.
captha: genius, aka, those who run.
Admin
If the code has been in C#/VB/whatever, the arguments would have been the same, but with somewhat different syntax.
This thread has nothing to do with Java; it has to do with an inept programmer, and their inability to use Java properly.
Admin
I haven't had a laugh like this in a few months. Thank you TDWTF and thank you God for stupid developers.
Some of the WTFs from the developers replying to the original WTF truly make this one of the ultimate WTFs. It's sort of like a recursive WTF. WTFs compounded on top of more and more WTFs.
Admin
If he's not yet competent enough to (a) write a trivial function and (b) recognise that there's a good chance it's already in the language, he's not reached an employable standard yet. Period.
If it's not tested, it doesn't work. Pretty much any developer learns that within about two weeks on the job, no matter how much they try to forget it afterwards. ;)
Erring once is fine, but this guy demonstrated that he couldn't actually learn from either his mistakes or the guidance of his peers. Everyone makes mistakes, but you can't better until you recognise them as mistakes.
Yes, because obviously it's much more important that developers have warm fuzzies and everybody feels confident and successful than it is to have working code...
I'm getting a strong sense that for you, "people skills" = "never making me feel bad" - if that's the case, don't ever expect to do anything critical... or even useful. It's not that you'll screw it up, necessarily; but that if you have, you'll neither believe it nor trust anyone to convince you of it - and that's dangerous, as this place illustrates all too regularly.
Not being unnecessarily cruel isn't the issue here; I'm all in favour of gentleness myself (I've been referred to as "the nicest person in the company" before, but I'm not sure that was a good thing) - but sometimes someone is just wrong, and it does nobody any good to pussyfoot around that.
Admin
Actually, running is probably about the worst thing you can do when faced with a large predator that is highly sensitive to motion and can run at well over twice your speed. Personally, I'd lie down and stay very still indeed. I might still get eaten, but if I run I'm sure to be lunch, so it's worth taking the chance.
Thing is, it's rather too late to buy a gun when the tiger's three feet away and looking like it hasn't eaten for a few days - the only thing that can save you at that point is knowing about tigers. Wouldn't you rather have bought a gun in preparation, instead of putting it off because you "didn't have time" until you really didn't have time...? And how much good is knowing about tigers going to do you when you're being carted off by a crocodile or hounded by hungry hyenas? Whereas guns can be fairly useful in those situations too.
Admin
Captcha: clueless.
Admin
Bad code, "Java sucks", strings, tigers, crocodiles and hyenas - it's getting more and more WTFy all the time. Let the WTF recursion continue! What iteration are we on? Will it ever stop?
Admin
"The real WTF is the common perception that unit testing wastes time rather than saving it. A software project that's in trouble "doesn't have time for unit testing". In the same way that person facing a tiger "doesn't have time buy a gun". "
I took this to mean that "If you are going in the jungle where there are tigers, make sure you have bought a gun in advance." That is, if you are putting your code into use, test it first.
Not that you should go buy a gun if a tiger happened upon you on a city street, where you wouldn't expect one. But if there were a gun store nearby, running into it might be a good idea.
Admin
I realize it's obvious that he did not test the function at all, but that's happened twice so far. The original coder, and the new person sent in to fix it.
I also wonder what exactly was explained to the new hire before he was sent off. If it was something like OMG it's gonna blow up fix it NOW!! Then I could see dumb stuff like this happening.
Another WTF could be that the production system is somehow hooked into a regular source code check in system? Where's the QA or unit tests to catch these things?
Admin
Admin
You people have too much time on your hands.
Admin
This has probably already been mentioned by now, but doesn't the "corrected" method return true if the argument is not null and is not a string? Very odd... it should probbaly throw an exception if the argument is not a string (if of course the method name bears any resembalence to it's true purpose)
Admin
Both snippets are magically difficult to understand for what little they do. Excellent obfuscation.
Admin
But null and !(instanceof String) have different return values. How is that redundant?
Admin
The real WTF (yay, i'm the first to spot it) is that this new developer was given a task to develop a method with the signature:
boolean isEmptyString(Object)
Why should you be allowed to pass an arbitrary object to a method which is called "isEmptyString"?
Answer: Bad design.
If you're given BS requirements, you're free to write BS code. If somebody claims it doesn't work, ask them: "Which requirement is not satisfied?"
Admin
Coder unfriendly? You can say a lot of bad things about Java, but this is ridiculous, absurd, and wrong.
Java is a strongly-typed, garbage-collected, modern language with C-ish syntax, no pointer math, and a large "standard" library.
I'm not saying its the greatest language, calling it the #2 least friendly language is crazy.
Admin
Yup, the nested WTFs are always my favorite part!
Admin
Additional WTFs:
the "updated" forum software submits my post as though I were anonymously logged in, when actually i'm logged in as a registered member
the "updated" forum software never remembers me, no matter how many times I click "Remember me next time"
Admin
The WTFiness of the original code snippets and of some of the comments is profound. This whole thread has been one of the most entertaining and craptastic WTFathons I have ever had the pleasure of reading during my entire short existence, whatever that is.
Absolutely correct, but dontcha love it!? I love the fact that you can, say, pass in an array of "universes" (or as someone else wrote, "little green men") into this function "isEmptyString", and it will faithfully spew out true. This is brilliant and useful information! And null... Well, we gotta find out if that's an empty string or not! (I also love the fact that the flow in this trash is even just a bit tricky to follow. Obfuscation perfection. I tip my hat to that.)
Someone else wrote:
Yes! And some more than others. Of course, I hope you see the irony of this little WTF, but maybe you are being tongue in cheek. I don't want to be a WTF hypocrite, I know I have written my share of bad code, but don't insult the authors of this website. That's why we're all here, we have too much time on our hands and want to enjoy life by reading about the idiocy of others.
It's been a great WTFeplex. Rock on.
Admin
not true! this is Java, BTW, not C#... (but even there it is not true)
it is outright depressing how many developers (in fact most of them) only know half (if we are lucky...) of the language of their choice, be it Java, C# or whatever and then embarras themselves in the WTF-forums ridiculing others without really having more clue... (which is the biggest of all WTFs of course ;o)
the only one who got near a correct Java solution is TSK with this idiom:
public boolean isEmptyString(CharSequence string) { return "".equals(string); }
this would only allow types which implement CharSequence (String, StringBuffer, StringBuilder etc.) so you can't pass in an Integer or anything like that, like someone stated, and should work for all "string-like" types. this is a nice idea, but unfortunately (or rather fortunately, but I won't go into that discussion here...) it won't work like that, because String#equals() checks for the type of the passed-in reference and only compares to types of itself (i.e. String), thus e.g. isEmptyString(new StringBuilder("")); yields 'false', even though the StringBuilder is passed in an empty string and therefore also empty (but it is not of type String!).
so the simplest correct solution is
public boolean isEmptyString(String s) { return "".equals(s); }
you could basically use a method parameter of type 'Object' safely, which would not be much different from the first version with 'CharSequence'. the advantage of using 'String', however, is that you will not be allowed to pass in anything that is not a 'String' already at compile time (you can of course always call toString() on any reference ;o)
but else the line return "".equals(s); in fact really is the best solution:
it is not necessary (like many stated) to check for 'null' explicitely, because on the one hand equals() is called on the string literal, so you never will get a NullPointerException. on the other hand, the contract of equals() demands that it is to return 'false' if 'null' is passed in.
it is NOT true, that a new object is created each time. the call "".equals(s) does NOT translate to new String("").equals(s) (as even many learning books and "Java gurus" continue to claim). in fact it translates to the equivalent of new String("").intern().equals(s) this "internalizes" the String, i.e. looks into a pool of string literals maintained by the String class and always returns the same reference for all occurrences of "" in the code - the instance is only created if it is not already present (i.e. at the first occurrence of ""). this holds true for all string literals in the code (and they are called "literals", dammit, not "constants"!) (see String#intern()) and this, BTW, is done in C# as well... this can easily be verified by the following: if (new String("") == new String("")) ... yields 'false' (because there are 2 distinct objects) if ("" == "") ... yields 'true', because you effectively compare the internalized instance to itself.
and even if that was not the case: the current garbage collection algorithms are specifically designed to work well for short-lived object instances - no explicit reclaiming is performed for object instances that do not get past the so-called 'eden'-generation where instances are allocated originally, because they are already dead when this space is garbage collected and therefore not moved to a so-called 'survivor space'. after having moved surviving objects, the whole 'eden'-space is just cleared out - so short-lived objects are never even looked at ;o)
this is also called "infant mortality", see http://java.sun.com/developer/technicalArticles/Networking/HotSpot/index.html or http://java.sun.com/docs/hotspot/gc5.0/gc_tuning_5.html for details.
captcha: photogenic - yeah ;o)
Admin
I have to dissent... it's halfwitted developers that suck, not the language.
if you do not know your left from your right it does not matter in what language you commit your crimes... there are arguably simpler languages, but these are also for simpler purposes.
the Java core langauge is in fact very simple if you know a set of really (I mean really) important basics, which most developers simlpy don't - it is of course not sufficient to just write VB programs with java keywords... e.g. nearly no one here seems to know that literals in Java and C# are internalized (see my other post) it's the libraries with thousands of classes that account for the complexities.
but if someone can't write proper Java, he or she won't write proper C#, C++, C, Ruby, ... (insert your fav language here, except VB)
captcha: pizza - thanks a lot, now I'm all hungry... ;o)
Admin
sorry, I forgot... I wanted to clearify the different misconceptions, but of course TSK's idea was basically a good one: if you want to have a method that also compares "string like" CharSequence types, you'd use
public static boolean isEmptyStringLike(CharSequence s) { return "".equals(s.toString()); }
now, by calling toString() on the CharSequence reference, two 'Strings' are compared. all the other things said above of course still hold true, but now you can pass in StringBuffers etc:
isEmptyStringLike(new StringBuilder(""))
will yield 'true' now, but you still won't be able to pass "incompatible" types like Integer etc.
captcha: bedtime - soooo true...;o)
Admin
Makes you think he might have been better off just going with:
public static boolean isEmptyString( Object obj ) { return false; }
That's about the patience I have for C# or whatever this is written in.
Admin
Looks like the real WTF is the comments to this, seems like people have forgotten the third boolean value, file_not_found.
It's obvious the code should be (when corrected to return true for empty strings):
public static boolean isEmptyString( Object obj ) { if ( !(obj instanceof String) ) return file_not_found;
}
Admin
And I don't particularly want to work at a company with someone whose first act is to throw up on me.
Hey, somebody chose a moniker specifically to attack me (anonymously, in both cases)!
Fist!
Admin
Well... you could always do "".equalsIgnoreCase(obj) for that little touch of WTF flavor.
Captch: Whiskey - well it is almost 9am...
Admin
Well... you could always do "".equalsIgnoreCase(obj) for that little touch of WTF flavor.
Captcha: Whiskey - well it is almost 9am...
Admin
I have to laugh at all the
WTF #1 : This method has a bad name.
It returns false only when null or "" passed. This isn't what one thinks of when looking at a method called isEmpty()
WTF #2 : There are presumably no unit tests.
WTF #3 : Most of the other posters assuming they know what the code is meant to do and laughably rewriting it.
Don't feel bad though. This is really because of #1, having a bad name. This really illustrates that the best comment is usually method name itself.
Without context, we have no idea what this method is used for, and the best we can do here is rename the method. After reviewing real output, the real name ought to be something like
because that's what it does. Maybe by renaming it others will pick up that it's not doing what they think it should do.Admin
Oracle thinks that's right. Who are we to complain?
Admin
This is, by far, my favorite comment. (Probably the most insightful, WRT the article, as well.)
Admin
Actually Java interns string literals and keeps them in a symbol table which is all handled at compile time.
see: http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#intern() and http://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html#19369
Admin
Or call it with an empty string and it returns false (even the "corrected" version). Seems like you are really in denial.
Admin
WTF? "".equals(anyInteger) is always false, as expected. The cluelessness of comments on this forum is baffling.
Admin
Why bother with reinventing the wheel in the first place? I'd go with Apache Jakarta Common's StringUtils, and use the time saved to get a fresh cup of Java.
Admin
WTF? The cluelessness of comments such as this is baffling.
See the method declaration? It expects a CharSequence, and thus the method does not, repeat not, accept an integer to be passed in. Try to call it with one and your Java compiler is nice enough to issue an error.
Admin
The REAL wtf here is the poor commenting style, imo:
"A bunch of spaces is not an empty string" means something to this developer, but what about in a year's time, or 5 years, or a million? What about all the other developers who might look at this code? The code itself does not make any mentions of empty stringage, we only know what the comment is about because we saw the first version!
Oh the humanity!
Captcha - stfu .. ok I take the hint :\