Below are two implementations for the isBlank() method.  One is from the
Commons Lang and the other was coded by a colleague of mine and he claims
his way is better and faster.  

 

I would like to understand how or why his or the Commons approach is better.
Maybe one is not better, but I still would like to have an understanding of
how and/or why someone would choose one way over the other so I can further
my software development skillset.  Maybe there are ramifications one way or
the other that I'm not seeing and someone can shed some light on for me?

 

My only motivation for this question is to learn and understand this stuff
better.  Thank you for any explanation you can provide!!  I do appreciate
any assistance or lessons!!

 

//From Commons project

public static boolean isBlank(String str) {

        int strLen;

        if (str == null || (strLen = str.length()) == 0) {

            return true;

        }

        for (int i = 0; i < strLen; i++) {

            if ((Character.isWhitespace(str.charAt(i)) == false)) {

                return false;

            }

        }

        return true;

 

 

Versus this way of doing the test:

 

 

 

//Modified with faster logic according to one of our developers  

public static boolean isBlank(String str){

      int strLen;

      if ((str == null) || ((strLen = str.length()) == 0))  {

            return true;

      }

      else  {

      str = str.trim();

      if (str.length() == 0)  {

                  return true;

            }

            else        {

                  return false;

            }

      }

}

 

Reply via email to