• Mickey (unregistered)

    You gotta love pointless if statements.

  • (cs)

    First ? Funny how alot of buggy code will never execute because of statements like this. Since it can't be both null and blank at the same time on todays computers

  • Skizz (unregistered) in reply to pitchingchris
    pitchingchris:
    First ? Funny how alot of buggy code will never execute because of statements like this. Since it can't be both null and blank at the same time on todays computers
    Must be anticipating those quantum computers then.
  • odi (unregistered) in reply to pitchingchris
    pitchingchris:
    Since it can't be both null and blank at the same time on todays computers

    You know Oracle? I love the fact that an emptry string is null...

  • Dirk (unregistered) in reply to pitchingchris

    I think he just didn't know the throw keyword.

    He must have intended to do this:

    if (reportGroup == null) {
       throw new NullPointerException();
    }
    
  • Anonymous (unregistered)

    It's been a while since I've worked with Java, so I may be off here, but if I recall correctly, Java will throw a NPE when you call an instance method on a null object. Which means that second snippet is worse than pointless, it's completely broken.

  • dbt (unregistered)

    In defense of the second bit of code, it probably meant to do ||, not &&.

    Of course,

    if (string.equals("")) { string = ""; }

    is a no-op, so all you have to do is

    if (string == null) { string = ""; },

    but ....

  • Keith Hackney (unregistered) in reply to Dirk

    I would have thought he was trying to do this

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

    Although he could have done (assuming C#.NET):

    reportGroup = String.IsNullOrEmpty(reportGroup) ? String.Empty : reportGroup;

    captcha: CockKnocker

  • NS (unregistered)

    Can anyone explain why this is done?

    reportGroup.equals("")

    I know what it does (obviously), but I've picked up some maintenance work (C#/asp.net) and it is littered with them. Why does this happen so often? Is it in some example code somewhere that every new programmer reads?

    NS

  • Luke (unregistered) in reply to Keith Hackney

    String.IsNullOrEmpty() looks like a horribly error prone method.

    Imagine the likelihood of forgetting to use it instead of String.Equals() (or whatever C# has) in some code already relying heavily upon it.

  • Keith Hackney (unregistered) in reply to Luke

    Not sure what you mean Luke

  • trianglman (unregistered)

    Correct me if I'm wrong (as I don't know JSP), and I know you will, but that first one doesn't appear to me to be a WTF. If I am reading it correctly, the JS variable is first being set to either "true" or "false" (as strings) and then being converted to a boolean. I know that just shows how poorly typed JS is, and it could just as easily be done in code by using "hasPriv=='true'" where it needs the boolean value, but all in all, not a WTF and probably makes for smaller code going over the wire.

  • (cs) in reply to NS
    NS:
    Can anyone explain why this is done?

    reportGroup.equals("")

    I know what it does (obviously), but I've picked up some maintenance work (C#/asp.net) and it is littered with them. Why does this happen so often? Is it in some example code somewhere that every new programmer reads?

    NS

    Are you meaning to say that you don't understand why people want to know whether a string is empty in the first place, or you don't understand why they choose that particular method to test whether a string is empty?

  • Squeese (unregistered)

    Isnt it missing a closing parenthesis ")" at the end there?

    if ((reportGroup == null) && (reportGroup.equals("")) {..}
  • pointless (unregistered)

    why not add && new String("").length()==0 to the if while your at it

  • 1emming (unregistered)
    if ((reportGroup == null) && (reportGroup.equals("")) { reportGroup = ""; }
    That should be:
    	try{
    		if( reportGroup.equals("") && reportGroup.equalsIgnoreCase("")){
    			throw new Exception("empty");
    		}
    	}catch(Exception e){
    		if(e.getMessage().equals("empty")){
    			throw new Exception("Report group may not be empty");
    		}else{
    			throw new Exception("Report group is most likely 'null'");
    		}
    	}
    	//to be sure..
    	if(reportGroup==null)
    		throw new Exception("Report group is most likely 'null'");
    
  • Elmo (unregistered)

    "it could just as easily be done in code by using "hasPriv=='true'" where it needs the boolean value"

    WTF?

    Why not: var hasPriv = <%=hasPriv%>;

  • 1emming (unregistered)
    if ((reportGroup == null) && (reportGroup.equals("")) { reportGroup = ""; }

    That is a useless test, it should be:

    	try{
    		if( reportGroup.equals("") && reportGroup.equalsIgnoreCase("")){
    			throw new Exception("empty");
    		}
    	}catch(Exception e){
    		if(e.getMessage().equals("empty")){
    			throw new Exception("Report group may not be empty");
    		}else{
    			throw new Exception("Report group is most likely 'null'");
    		}
    	}
    	//to be sure..
    	if(reportGroup==null || reportGroup.length()<0)
    		throw new Exception("Report group is most likely 'null', or the length is not enough
    
  • 1emming (unregistered)

    I forgot, the code i posted should be wrapped in a try/catch, and on the catch we should do something like:

    catch(Exception e){ log.write("warning, we set reportGroup to "" because something went wrong.. I think"); reportGroup=""; }

  • Grant (unregistered) in reply to trianglman

    Um. If the server-side hasPriv is a boolean that evaluates to true or false, then I fail to see how:

    var hasPriv = "<%= hasPriv %>"; hasPriv = eval(hasPriv);

    is "smaller code going over the wire" than:

    var hasPriv = <%= hasPriv %>; // ex.: var hasPriv = true;

    As for your comment about client-side JavaScript being "badly typed". You can very easily set a variable to true or false, you don't need to set a string to "true" and compare it to 'true'. Just goes to show you're complaining about a language you don't even understand.

  • (cs)

    I recently hit this piece of pointless code in our own codebase:

    public static final boolean isEmpty(String value){
    return value.trim().equals("") || value == null;
    }
    When checking to see if something is null, is it better late than never? Also note the amazingly pointless use of final on a static method.

  • (cs) in reply to Keith Hackney
    Keith Hackney:
    I would have thought he was trying to do this

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

    Although he could have done (assuming C#.NET):

    reportGroup = String.IsNullOrEmpty(reportGroup) ? String.Empty : reportGroup;

    captcha: CockKnocker

    reportGroup = reportGroup ?? string.Empty;

  • (cs) in reply to Squeese
    Squeese:
    Isnt it missing a closing parenthesis ")" at the end there?
    if ((reportGroup == null) && (reportGroup.equals("")) {..}

    Yes, thanks. (Fixed.)

  • (cs) in reply to Mickey
    Mickey:
    You gotta love pointless if statements.

    My Java-Fu is admittedly weak, but my first thought was "did he want an OR there instead of AND?"

  • dkf (unregistered) in reply to XIU
    XIU:
    reportGroup = reportGroup ?? string.Empty;
    That one makes me wonder if there is a ?! operator (the WTF?! operator) too...
  • (cs) in reply to dkf
    dkf:
    XIU:
    reportGroup = reportGroup ?? string.Empty;
    That one makes me wonder if there is a ?! operator (the WTF?! operator) too...

    Maybe for C# 3, who knows...

  • (cs) in reply to dkf
    dkf:
    XIU:
    reportGroup = reportGroup ?? string.Empty;
    That one makes me wonder if there is a ?! operator (the WTF?! operator) too...
    Syntax of this operator is as follows:
    condition ?! ifTrue : ifFalse : fileNotFound
  • snoofle (unregistered) in reply to Thuktun
    Thuktun:
    I recently hit this piece of pointless code in our own codebase:
    public static final boolean isEmpty(String value){
      return value.trim().equals("") || value == null;
    }
    When checking to see if something is null, is it better late than never? Also note the amazingly pointless use of final on a static method.
    What about the anti-static Cling-Free (TM)? - you can never really be sure how final something is unless you explcitly specify it, and even then....
  • Hel (unregistered) in reply to odi

    To know Oracle is to Hate Oracle!. To know NVL() is to hate Oracle a little less...

  • Hel (unregistered) in reply to odi
    odi:
    pitchingchris:
    Since it can't be both null and blank at the same time on todays computers

    You know Oracle? I love the fact that an emptry string is null...

    Well, that comment was totally useless without the quote, so here goes again...

    To know Oracle is to hate Oracle. To know NVL() is to hate oracle a little less...

  • I like your thinking (unregistered) in reply to snoofle
    public static final boolean isEmpty(String value){
      return value.trim().equals("") || value == null;
    }
    Let's remember the other boolean values as well:
      if (x == sortOfNull     || // randomly has value
          x == prettyNull     || // has little value
          x == nullerThanMost || // mostly null
          x == null           || // absolute-null
          x ?! null : itsDeadJim : itsAliveJim : itJustWontDieJim) {
         ...
      }
    
  • (cs)

    The code sounds perfectly reasonable for me. After all, what would all those pretty optimizing compilers with dead code/pointless if detection be good for if no one wrote code for them...?

    (just to make it clear: </SARCASM!!!>)

  • hmm... (unregistered) in reply to XIU

    ?? only evaluate null value. so it misses the original authors wish to evaluate ""

  • Jim (unregistered) in reply to Welbog

    This is more idiomatic in Java:

    if (reportGroup == null || reportGroup.length() == 0) { // Handle null case }

  • (cs) in reply to Elmo

    Why not: var hasPriv = <%=hasPriv%>;

    The right-hand hasPriv is a Java boolean. What if somebody forgot to assign a value to it (or its value is fileNotFound)?

    The result would be "var hasPriv = ;" which is invalid JavaScript code.

    Myself, I would have checked to see if (Java) hasPriv was set before trying to use it in the (Javascript) hasPriv assignment. Or maybe done something upstream to confirm that hasPriv was always set before that point.

  • Aaron (unregistered) in reply to hmm...
    hmm...:
    ?? only evaluate null value. so it misses the original authors wish to evaluate ""
    Sarcasm?

    All that the original code does if reportGroup is the empty string ("") is set it to the empty string. Thus he doesn't really care if it's already an empty string - he just wants null to be replaced with empty.

    The null evaluator works perfectly fine for this. No need to check for an empty string when you're not going to do anything with it.

  • Aaron (unregistered) in reply to Rootbeer
    Rootbeer:
    Why not: var hasPriv = <%=hasPriv%>;

    The right-hand hasPriv is a Java boolean. What if somebody forgot to assign a value to it (or its value is fileNotFound)?

    The result would be "var hasPriv = ;" which is invalid JavaScript code.

    Myself, I would have checked to see if (Java) hasPriv was set before trying to use it in the (Javascript) hasPriv assignment. Or maybe done something upstream to confirm that hasPriv was always set before that point.

    Since when is boolean a nullable type? If it isn't set, it will evaluate to the default value (false) when the page is rendered.
  • (cs) in reply to Aaron
    Aaron:
    Rootbeer:
    Why not: var hasPriv = <%=hasPriv%>;

    The right-hand hasPriv is a Java boolean. What if somebody forgot to assign a value to it (or its value is fileNotFound)?

    The result would be "var hasPriv = ;" which is invalid JavaScript code.

    Myself, I would have checked to see if (Java) hasPriv was set before trying to use it in the (Javascript) hasPriv assignment. Or maybe done something upstream to confirm that hasPriv was always set before that point.

    Since when is boolean a nullable type? If it isn't set, it will evaluate to the default value (false) when the page is rendered.

    How about Oracle?

  • WTFNamingException (unregistered) in reply to DigitalXeron

    [quote user="DigitalXeron]How about Oracle?[/quote] No thanks!

  • Malfist (unregistered) in reply to Keith Hackney
    Keith Hackney:
    I would have thought he was trying to do this

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

    Although he could have done (assuming C#.NET):

    reportGroup = String.IsNullOrEmpty(reportGroup) ? String.Empty : reportGroup;

    captcha: CockKnocker

    Or is java: reportGroup = (reportGroup == null || reportGroup.equals("")) "" : reportGroup

  • tastey (unregistered)

    I'm just curious which multi-billion dollar company was taken to its knees because this seemingly useless bit of code was actually relevant for the core applications to run properly.

  • Xero (unregistered) in reply to Thuktun
    Thuktun:
    I recently hit this piece of pointless code in our own codebase:
    public static final boolean isEmpty(String value){
      return value.trim().equals("") || value == null;
    }
    When checking to see if something is null, is it better late than never? Also note the amazingly pointless use of final on a static method.

    Why do you think final on a static is pointless?
    Without the final, any class which extends this class can implement a method with the same signature which gets really confusing if someone were to call [instance of child class].isEmpty(). I will admit that calling a static method on an instance is bad practice, but is is possible and you never know what the new CS grad the company hires to support you is going to try and do.

  • (cs) in reply to NS
    NS:
    Can anyone explain why this is done?

    reportGroup.equals("")

    I know what it does (obviously), but I've picked up some maintenance work (C#/asp.net) and it is littered with them. Why does this happen so often? Is it in some example code somewhere that every new programmer reads?

    NS

    The first thing you learn in Java 101 is how to use == to compare two ints.

    Then, they ask you to compare two Strings, and you learn that == doesn't work on objects like you might expect, and you're taught to use String.equals() to compare strings.

    Now, using this basic knowledge, you need to test a string to see if it's empty. Logically, you use String.equals("") -- it's the string comparator you're most familiar with.

    Using String.length > 0 would probably be better, but they don't teach you that on the first day of Java 101.

  • iMalc (unregistered)

    I don't suppose a second thread was constantly toggling reportGroup between NULL and empty string. Hmm, I guess not. Just thinking outside the box.

  • bobbo (unregistered)

    I've only been hanging around here for a couple of months, but this particular comments page is the best collection of obvious-stating, point-missing and non-reading-of-article I've seen yet.

  • Tyrathect (unregistered) in reply to bobbo

    Isn't

    (exp) ?? a : b

    syntactic sugar for:

    if (((exp) == true) || ((exp) != false)) return a; return b;

    captcha: Doom (ain't that the truth)

  • Jon (unregistered) in reply to XIU
    XIU:
    Keith Hackney:
    reportGroup = String.IsNullOrEmpty(reportGroup) ? String.Empty : reportGroup;
    reportGroup = reportGroup ?? string.Empty;
    reportGroup = reportGroup ?? "";

    string.Empty is the real WTF.

  • brendan (unregistered)

    the first ones solved like this

    
    

    the second one is solved like this.

    if (reportGroup == null)
       reportGroup = "";
    
  • mage (unregistered)

    I saw code like this all over an ASP application that I was working on for years. Until that fateful day when a German customer started experiencing problems. As it turns out, casting a boolean to a string in German is the word "falsh" and casting that back again to a boolean on an english web server turns out to be BOOM!

    -K

  • Nonymous (unregistered) in reply to Malfist
    Malfist:
    Or is java: reportGroup = (reportGroup == null || reportGroup.equals("")) "" : reportGroup

    You're missing a ? in your ternary expression.

Leave a comment on “Double Take”

Log In or post as a guest

Replying to comment #:

« Return to Article