• Phoenix (unregistered)

    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:

     public static boolean isEmptyString( Object obj )
     {
       return (obj instanceof String&&((String)obj).length()==0)
    }
    

    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.

  • G-Unit (unregistered) in reply to Winter

    Ooh, a new, more efficient wheel?

  • G-Unit (unregistered) in reply to Winter

    "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?

  • Zylon (unregistered)

    It's threads like this that reaffirm my conviction that Java really, really sucks.

  • Michael (unregistered) in reply to Zylon
    Zylon:
    It's threads like this that reaffirm my conviction that Java really, really sucks.

    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?

  • (cs) in reply to Michael
    Michael:
    Because people who don't know about Java are making incorrect statements and assumptions about Java, it sucks?
    Because such an excruciatingly trivial task has already sparked over a page of debate on the "right" way to do it.

    Barring Ada, Java is pretty much at the top of my list for coder-unfriendly languages.

  • AdderTheBlack (unregistered)

    I have no idea what this code is trying to do; absolutely none. Please fix the code below to taste.

    public static boolean isEmptyString( Object obj )
    {
        if ( obj == null )
            return false;
    
        if ( ! ( obj instanceof String ) )
            return false; // or true? no idea
    
        /* a bunch of spaces is not an empty string */
        return ( ((String)obj).length() == 0 );
        /* a bunch of spaces *is* an empty string */
        //return ( ((String)obj).trim().length() == 0 );
    }
    

    Now.. What is an appropriate name?

  • Michael (unregistered) in reply to Zylon
    Zylon:
    Michael:
    Because people who don't know about Java are making incorrect statements and assumptions about Java, it sucks?
    Because such an excruciatingly trivial task has already sparked over a page of debate on the "right" way to do it.

    Barring Ada, Java is pretty much at the top of my list for coder-unfriendly languages.

    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#?

  • (cs) in reply to sriram
    sriram:
    is it java or is it .NET they all seem the same to me. i googled and found out it was java only. .NET uses typeOf.

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

  • barfing on real_aardvark (unregistered) in reply to real_aardvark

    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.

  • hmmm. (unregistered)

    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.

  • Michael (unregistered) in reply to hmmm.
    hmmm.:
    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.

    I think the WTF is that they did explain to him what he did wrong, and he still didn't get it.

  • sir_flexalot (unregistered) in reply to Michael

    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?"

  • (cs) in reply to barfing on real_aardvark
    barfing on real_aardvark:
    (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.

    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".

  • Michael (unregistered) in reply to arty
    arty:
    barfing on real_aardvark:
    (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.

    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".

    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.

  • been there (unregistered) in reply to Zylon
    Zylon:
    It's threads like this that reaffirm my conviction that Java really, really sucks.

    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.

  • Alex (unregistered)

    "WTFy"

    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.

  • (cs) in reply to barfing on real_aardvark
    barfing on real_aardvark:
    Mind you, the real WTF is when some commenter replies when (a) he assumes this guy is an experienced dev. who the f knows?

    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.

    (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.

    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. ;)

    (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?

    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.

    The fact that the second version of the code makes you head spin means you are someone I would dread to work with.

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

    The real WTF are companies that hire pretentious asses who make your work life a living hell, and have no ppl skills whatsoever.

    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.

  • (cs) in reply to Michael
    Michael:
    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.

    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.

  • IQpierce (unregistered) in reply to hanno
    hanno:
    OK now I got it. Call it with an Integer and it returns true. Sometimes your mind wants to refuse to see how bad things are.
    What about the fact that he was obviously told "hey, this function should return TRUE if the string contains only spaces"; and that he added a comment to that effect; and that the function still completely does not do that?

    Captcha: clueless.

  • Alex (unregistered)

    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?

  • nobody (unregistered) in reply to arty

    "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.

  • Russ (unregistered)
    From the Article...:
    The unfortunate part is that this code was checked in on production. Luckily, another developer caught it. The new hire --- after having some things explained to him --- was sent off to fix it.

    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?

  • (cs) in reply to TSK
    TSK:
    Gosh !

    public boolean isEmptyString(CharSequence cs) { if (cs == null) return false; return "".equals(cs.toString()); }

    Sorry for risking a big lip...

    In Java 5 and up, the last line could also be return "".contentEquals(cs);

  • Cliff (unregistered)

    You people have too much time on your hands.

  • me (unregistered) in reply to Cliff

    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)

  • Corporate Cog (unregistered)

    Both snippets are magically difficult to understand for what little they do. Excellent obfuscation.

  • (cs) in reply to nmx
    nmx:
    (x == null) followed by (x instanceof String) is redundant. instanceof implicitly checks against null.

    But null and !(instanceof String) have different return values. How is that redundant?

  • (unregistered) in reply to Araqnid

    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?"

  • (cs) in reply to Zylon
    Zylon:
    Michael:
    Because people who don't know about Java are making incorrect statements and assumptions about Java, it sucks?
    Because such an excruciatingly trivial task has already sparked over a page of debate on the "right" way to do it.

    Barring Ada, Java is pretty much at the top of my list for coder-unfriendly languages.

    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.

  • (cs) in reply to Alex
    Alex:
    >"WTFy"

    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.

    Yup, the nested WTFs are always my favorite part!

  • (cs) in reply to
    :
    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?"

    Additional WTFs:

    1. the "updated" forum software submits my post as though I were anonymously logged in, when actually i'm logged in as a registered member

    2. the "updated" forum software never remembers me, no matter how many times I click "Remember me next time"

  • Alex (unregistered)

    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.

    Why should you be allowed to pass an arbitrary object to a method which is called "isEmptyString"? Answer: Bad design.

    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:

    You people have too much time on your hands.

    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.

  • woohoo (unregistered) in reply to AndrewB
    AndrewB:
    "".equals(whatever)

    is technically not optimal because it unnecessarily wastes processor time constructing a string object ("") when one may very well already be constructed for us.

    Don't reinvent the wheel!

    String.Empty.equals(whatever)

    Also, here is the solution to the original solution

    public boolean isEmptyStringOhAndUseThisFunctionAndNotIsEmptyStringBecauseItIsBroken(Object obj) { return !isEmptyString(obj); }

    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:

    1. 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.

    2. 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)

  • woohoo (unregistered) in reply to Zylon
    Zylon:
    It's threads like this that reaffirm my conviction that Java really, really sucks.

    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)

  • woohoo (unregistered) in reply to woohoo
    woohoo:
    AndrewB:
    "".equals(whatever)

    is technically not optimal because it unnecessarily wastes processor time constructing a string object ("") when one may very well already be constructed for us.

    Don't reinvent the wheel!

    String.Empty.equals(whatever)

    Also, here is the solution to the original solution

    public boolean isEmptyStringOhAndUseThisFunctionAndNotIsEmptyStringBecauseItIsBroken(Object obj) { return !isEmptyString(obj); }

    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:

    1. 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.

    2. 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)

    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)

  • clayne (unregistered)

    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.

  • (cs)

    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;

    if ( ((String)obj).length() == 0 )
        return true;
    
    return false;
    

    }

  • (cs) in reply to gwenhwyfaer
    barfing on real_aardvark:
    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.
    Umm, all I actually assumed was that Alex hasn't obfuscated the details to the point of incomprehensibility. The guy is one of many "consultants" (I agree that this might refer to a different field, but for now let's assume computers) and is hired full-time. I think it's legitimate to assume that he is meant at least to understand the concept of unit-testing fundamental methods, or even looking them up in documentation. I would also assume that a company hiring large numbers of consultants isn't a Mom and Pop operation, unless Pop is the Godfather and Mom is the Fairy Godmother.
    barfing on real_aardvark:
    (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.
    Um, again. Please re-read (c). I would certainly make him rewrite it. The point is, I would also insist on it being tested this time. I don't know how often you like to play Russian Roulette, but, personally, the second time I put the gun to my head, I would prefer to check the chamber for bullets beforehand.
    barfing on real_aardvark:
    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.
    On the assumption that you are not someone you would dread to work with; unhelpful; spiteful; and ego-driven -- all of which is an interesting deduction from a thirteen-word sub-clause -- this means that you understand the second version of the code. Or that your head doesn't spin when you encounter unintelligible gibberish. I believe Microsoft and Sun hand out certificates for this level of proficiency.
    barfing on real_aardvark:
    The real WTF are companies that hire pretentious asses who make your work life a living hell, and have no ppl skills whatsoever.
    gwenhwyfaer:
    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.

    Well, I didn't think I was being cruel!

    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!

  • Matt V (unregistered) in reply to been there

    Well... you could always do "".equalsIgnoreCase(obj) for that little touch of WTF flavor.

    Captch: Whiskey - well it is almost 9am...

  • Matt V (unregistered) in reply to been there

    Well... you could always do "".equalsIgnoreCase(obj) for that little touch of WTF flavor.

    Captcha: Whiskey - well it is almost 9am...

  • George (unregistered)

    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

    IsNotNullOrNotEmptyString
    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.

    null -> false
    "" -> false
    
    " " -> true
    1 -> true
    3.14 -> true
    new WTF() -> true
    
  • (cs) in reply to Moogle
    Moogle:
    Several people have commented that the code is fine other than the name reversal, yet no one had pointed out that both versions of the function return the same value for null and an empty string.

    WTF?

    Oracle thinks that's right. Who are we to complain?

  • (cs) in reply to tharfagreinir

    This is, by far, my favorite comment. (Probably the most insightful, WRT the article, as well.)

    tharfagreinir:
    To quote Shakespeare:
    It is code written by an idiot, full of conditional statements and casts, validating nothing.
  • Anonymous (unregistered) in reply to AndrewB

    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

  • AdT (unregistered) in reply to hanno
    hanno:
    OK now I got it. Call it with an Integer and it returns true. Sometimes your mind wants to refuse to see how bad things are.

    Or call it with an empty string and it returns false (even the "corrected" version). Seems like you are really in denial.

  • AdT (unregistered) in reply to Chuggid
    Chuggid:
    TSK:
    public boolean isEmptyString(CharSequence string) { return "".equals(string); }
    Doesn't handle the case of passing in, say, an Integer, which I take it the method is supposed to.

    WTF? "".equals(anyInteger) is always false, as expected. The cluelessness of comments on this forum is baffling.

  • Angel (unregistered)

    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.

  • (cs) in reply to AdT
    AdT:
    Chuggid:
    TSK:
    public boolean isEmptyString(CharSequence string) { return "".equals(string); }
    Doesn't handle the case of passing in, say, an Integer, which I take it the method is supposed to.

    WTF? "".equals(anyInteger) is always false, as expected. The cluelessness of comments on this forum is baffling.

    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.

  • Peter (unregistered)

    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 :\

Leave a comment on “Validating Nothing”

Log In or post as a guest

Replying to comment #111296:

« Return to Article