Boolean isBlank(String str)



  •  <font face="Verdana" size="2">I came across this
    beauty while reviewing the code for a temporary project.</font>

     
    <font face="Verdana" size="2">The code is self explanatory. Guess what happens if input string contains 6 blank spaces??? LOL
    </font>
     
    <font face="Verdana" size="2"> /**
      * This method checks to see if any of the input fields are blank spaces.
      * Creation date: (1/3/01 3:12:19 PM)
      *
      * @return boolean
      * @param param
      *            java.lang.String
      */
     public boolean isBlank(String str) {
      if (str.equals("") || str.equals(" ") || str.equals("  ")
        || str.equals("   ") || str.equals("    ")
        || str.equals("     "))
       return true;
      else
       return false;
     }// end
    </font>



  • <font size="2"><font face="Verdana">does the words dot contains mean anything to the developer who wrote this?</font></font><font face="Verdana" size="2"></font>



  • So he's using a version of Java with no Trim() function?



  • Nice.  Even the comment is wrong -- it's not checking any, it's checking all (for values of "all" <= 5).



  • @yankeey2g said:

     <font size="2" face="Verdana">I came across this
    beauty while reviewing the code for a temporary project.</font>

     
    <font size="2" face="Verdana">The code is self explanatory. Guess what happens if input string contains 6 blank spaces??? LOL
    </font>
     
    <font size="2" face="Verdana"> /**
      * This method checks to see if any of the input fields are blank spaces.
      * Creation date: (1/3/01 3:12:19 PM)</font>
    <font size="2" face="Verdana">  * (15/9/01) Added support for two spaces</font>.
    <font size="2" face="Verdana">  * (5/3/03) Added support for three spaces.</font>
    <font size="2" face="Verdana">  * (9/6/09)</font> Made method generic as per issue #24673. Method is now generic up to and including five spaces.
    <font size="2" face="Verdana">  *
      * @return boolean
      * @param param
      *            java.lang.String
      */
     public boolean isBlank(String str) {
      if (str.equals("") || str.equals(" ") || str.equals("  ")
        || str.equals("   ") || str.equals("    ")
        || str.equals("     "))
       return true;
      else
       return false;
     }// end
    </font>

     

    FTFY



  • @heffkat said:

    <font size="2"><font face="Verdana">does the words dot contains mean anything to the developer who wrote this?</font></font>
    Why? What's your point?

    @cconroy said:

    Nice.  Even the comment is wrong -- it's not checking any, it's checking all (for values of "all" <= 5).
    The comment is wrong, but it's because there are no input fields involved, just a string.



  • @yankeey2g said:

     <font face="Verdana" size="2">I came across this
    beauty while reviewing the code for a temporary project.</font>

     
    <font face="Verdana" size="2">The code is self explanatory. Guess what happens if input string contains 6 blank spaces??? LOL
    </font>
     
    <font face="Verdana" size="2"> /**
      * This method checks to see if any of the input fields are blank spaces.
      * Creation date: (1/3/01 3:12:19 PM)
      *
      * @return boolean
      * @param param
      *            java.lang.String
      */
     public boolean isBlank(String str) {
      if (str.equals("") || str.equals(" ") || str.equals("  ")
        || str.equals("   ") || str.equals("    ")
        || str.equals("     "))
       return true;
      else
       return false;
     }// end
    </font>

    I have a similar story, one of my coworkers was asked to normalize the spaces in a string.  We even explained exactly what "normalize" means.  He came up with this one-line classic (yes it's in VB6):

    strVal = Replace(strVal, "  ", " ")

    Ummm... While Loop, anyone?



  • @CodeWatcher said:

    @yankeey2g said:

     <font face="Verdana" size="2">I came across this
    beauty while reviewing the code for a temporary project.</font>

     
    <font face="Verdana" size="2">The code is self explanatory. Guess what happens if input string contains 6 blank spaces??? LOL
    </font>
     
    <font face="Verdana" size="2"> /**
      * This method checks to see if any of the input fields are blank spaces.
      * Creation date: (1/3/01 3:12:19 PM)
      *
      * @return boolean
      * @param param
      *            java.lang.String
      */
     public boolean isBlank(String str) {
      if (str.equals("") || str.equals(" ") || str.equals("  ")
        || str.equals("   ") || str.equals("    ")
        || str.equals("     "))
       return true;
      else
       return false;
     }// end
    </font>

    I have a similar story, one of my coworkers was asked to normalize the spaces in a string.  We even explained exactly what "normalize" means.  He came up with this one-line classic (yes it's in VB6):

    strVal = Replace(strVal, "  ", " ")

    Ummm... While Loop, anyone?

    How is that wrong? Does "Replace()" in that language mean "ReplaceOnce()" or "ReplaceAll()"? If the latter, maybe I don't get what you mean by "normalize".

    BTW, why doesn't PHP include any form of ReplaceOnce() without having to use RegEx? That's a giant PITA.



  • @blakeyrat said:

    @CodeWatcher said:

    I have a similar story, one of my coworkers was asked to normalize the spaces in a string. 
    We even explained exactly what "normalize" means.  He came up with this
    one-line classic (yes it's in VB6):

    strVal = Replace(strVal, "  ", " ")

    Ummm... While Loop, anyone?

    How is that wrong? Does "Replace()" in that language mean "ReplaceOnce()" or "ReplaceAll()"? If the latter, maybe I don't get what you mean by "normalize".

    BTW, why doesn't PHP include any form of ReplaceOnce() without having to use RegEx? That's a giant PITA.

     

    From the context, it looks like he means reducing any instance of multiple spaces down to single spaces, which is itself a massive WTF, and one of the biggest defects of HTML that they've never actually done anything to fix.  (No, "code" tags and nbsps are not real fixes.)



  • Normalize means to reduce occurrences of multiple spaces to a single space, so whether you have 2, 3, or 20 spaces in a row, after normalizing it should be 1 space.  Without some kind of loop, that single REPLACE statement will not work correctly on more than 2 spaces in a row in the original string:  a sequence of 3 or 4 spaces becomes 2 spaces, 5 or 6 spaces becomes 3, etc.

    Actually it doesn't matter that this was written for VB... now that I think about it, the plain vanilla (i.e. "non-regex") String Replace in most languages won't do this correctly.  I would've done it with regex using greedy matching.  Or just put it in a loop, which is what we ended up doing.



  • @CodeWatcher said:

    Normalize means to reduce occurrences of multiple spaces to a single space, so whether you have 2, 3, or 20 spaces in a row, after normalizing it should be 1 space.  Without some kind of loop, that single REPLACE statement will not work correctly on more than 2 spaces in a row in the original string:  a sequence of 3 or 4 spaces becomes 2 spaces, 5 or 6 spaces becomes 3, etc.

    It will with the default PHP Replace() behavior. I know this because I ran across an extremely similar scenario in PHP just this weekend, which is also what prompted the little rant about PHP's lack of ReplaceOnce(). You didn't provide enough information to determine it's WTFicality.



  • @blakeyrat said:

    @CodeWatcher said:
    Normalize means to reduce occurrences of multiple spaces to a single space, so whether you have 2, 3, or 20 spaces in a row, after normalizing it should be 1 space.  Without some kind of loop, that single REPLACE statement will not work correctly on more than 2 spaces in a row in the original string:  a sequence of 3 or 4 spaces becomes 2 spaces, 5 or 6 spaces becomes 3, etc.

    It will with the default PHP Replace() behavior. I know this because I ran across an extremely similar scenario in PHP just this weekend, which is also what prompted the little rant about PHP's lack of ReplaceOnce(). You didn't provide enough information to determine it's WTFicality.


    PHP has no Replace(). Only str_replace().

    [robin@George1 ~]$ cat test.php 
    <?php
            $str = '                                                                                                 This is a             fucking              load of          spaces!' . PHP_EOL;
            print $str;
            print str_replace('  ', ' ', $str);
            print str_replace(array('  ','  '), ' ', $str);
            print str_replace(array('  ','  '), array(' ',' '), $str);
    [robin@George1 ~]$ php test.php
                                                                                                     This is a             fucking              load of          spaces!
                                                     This is a       fucking       load of     spaces!
                             This is a    fucking    load of   spaces!
                             This is a    fucking    load of   spaces!
    [robin@George1 ~]$ 
    


  • @Lingerance said:

    @blakeyrat said:
    @CodeWatcher said:
    Normalize means to reduce occurrences of multiple spaces to a single space, so whether you have 2, 3, or 20 spaces in a row, after normalizing it should be 1 space.  Without some kind of loop, that single REPLACE statement will not work correctly on more than 2 spaces in a row in the original string:  a sequence of 3 or 4 spaces becomes 2 spaces, 5 or 6 spaces becomes 3, etc.

    It will with the default PHP Replace() behavior. I know this because I ran across an extremely similar scenario in PHP just this weekend, which is also what prompted the little rant about PHP's lack of ReplaceOnce(). You didn't provide enough information to determine it's WTFicality.


    PHP has no Replace(). Only str_replace().

    [robin@George1 ~]$ cat test.php 
    <?php
            $str = '                                                                                                 This is a             fucking              load of          spaces!' . PHP_EOL;
            print $str;
            print str_replace('  ', ' ', $str);
            print str_replace(array('  ','  '), ' ', $str);
            print str_replace(array('  ','  '), array(' ',' '), $str);
    [robin@George1 ~]$ php test.php
                                                                                                     This is a             fucking              load of          spaces!
                                                     This is a       fucking       load of     spaces!
                             This is a    fucking    load of   spaces!
                             This is a    fucking    load of   spaces!
    [robin@George1 ~]$ 
    

    First of all, don't be a pedant, you know which function I meant. Christ.

    Then I guess the bug is in the docs:

    @PHP Docs said:

    Caution

    Replacement order gotcha

    Because str_replace() replaces left to right, it might replace a previously inserted value when doing multiple replacements. See also the examples in this document.

    Which makes me wonder what was going wrong with my code, because what I was seeing matched the docs, not your example. Oh well, moot now.



  • @Lingerance said:

    PHP has no Replace(). Only str_replace().

    PHP has also str_ireplace(), substr_replace() and strtr().



  •  @blakeyrat said:

    It will with the default PHP Replace() behavior. I know this because I ran across an extremely similar scenario in PHP just this weekend, which is also what prompted the little rant about PHP's lack of ReplaceOnce(). You didn't provide enough information to determine it's WTFicality.

    Well I mentioned (in passing) that this was VB6, so we actually have 4x WTFicality:  VB6 itself, using VB6 in the year 2010, the code doesn't work, and our developer didn't test it with more than 2 consecutive spaces.  Pity me, I deal with stuff like this on a daily basis.  Oh and TRWTF is that this guy isn't much better in .Net.



  • @heffkat said:

    <font size="2"><font face="Verdana">does the words dot contains mean anything to the developer who wrote this?</font></font>
    Do they mean anything to you, because they certainly won't solve this problem any easier.



  • @heffkat said:

    <font size="2"><font face="Verdana">does the words dot contains mean anything to the developer who wrote this?</font></font><font face="Verdana" size="2"></font>

    if("  Hi, I'm blank ".contains(" ")) {
        System.out.println("Yup, it's totally blank.");
    }
    else {
        System.out.println("Nope, not blank");
    }
    


  •  Maybe all the fields in his application have a five character limit. Ok, this would be no excuse for the aberration he did, I'm just trying to rationalize how someone could do something like that.



  • Wow, someone probably paid a good chunk of change for that bullshit code.



  • [quote user="Renan "C#" Sousa"] Maybe all the fields in his application have a five character limit. Ok, this would be no excuse for the aberration he did, I'm just trying to rationalize how someone could do something like that. [/quote] 

    You're trying to rationalise this code but a rational mind would be thinking things like "Surely there's a better way to do this" and "What happens when the field length changes to 6 or 10 or 30 characters".

    I do note that you're not trying to excuse it though!

    If I had to try to guess how this happened I'd say it was at the point in the developer's career when he/she knew about comparisons but not loops.



  • @toth said:

    @heffkat said:
    <font size="2"><font face="Verdana">does the words dot contains mean anything to the developer who wrote this?</font></font><font face="Verdana" size="2"></font>


    if(" Hi, I'm blank ".contains(" ")) {
    System.out.println("Yup, it's totally blank.");
    }
    else {
    System.out.println("Nope, not blank");
    }

    Obviously that doesn't work.
    You need to do
    if(!str.contains("a") &&
       !str.contains("b") &&
       !str.contains("c") &&
       *snip* &&
       !str.contains("A") &&
       *snip* &&
       !str.contains("1") &&
       *snip* &&
       !str.contains("~") &&
       *snip*) {
        System.out.println("Yup, it's totally blank.");
    }
    else {
        System.out.println("Nope, not blank");
    }
    
    Or you could optimise it with !str.toLower().contains("a") - then you can leave out the 26 tests for capitals!


  • @aihtdikh said:

    Or you could optimise it with
    !str.toLower().contains("a")

    • then you can leave out the 26 tests for capitals!

    πολύ καλά!



  • Wait, I think I got it:


    public boolean isBlank(String str) {
       for(int i = 0; i < str.length; ++i)
       {
          if( !str.substring(i, i).contains(" ") ) return false;
       }
       return true;
    }



  • Wait! multiple return values are confusing:

     

    public boolean isBlank(String str) {
      int falce = false;
      for(int i = 0; i < str.length; ++i)
      {
        if( !str.substring(i, i).contains(" ") ) falce = true;
      }
      return !falce;
    }
    


  • I don't like to do this as a boolean.

    public boolean getBlankness(String str) {

        int falce = 0;

        for(int i = 0; i < str.length; ++i) {

            if( !str.substring(i, i).contains(" ") ) falce++;

        }

        // crash on empty strings. this is done intentionally

        return falce/str.length;

    }

     



  • I dunno about that crash..

     

    public boolean getBlankness(String str) {

        int falce = 0;

        for(int i = 0; i < str.length; ++i) {

            if( !str.substring(i, i).contains(" ") ) falce++;

        }

        str += " ";

    falce += true;

     

        return falce/str.length;

    }



  • I don't know about that invalid cast exception.

    public double getBlankness(String str) {

    int falce = 0;
    
    
    for(int i = 0; i &lt; str.length; ++i)  {
    
    
        if( !str.substring(i, i).contains(" ") ) falce++;
    
    
    }
    
    
    str += " ";
    
    falce += true;
    
    
    
    return falce/str.length;
    

    }


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.