Another faulty example . . .

More Unfair Language Comparisons

by Conrad Weisert, May 1, 2017
©2017 Information Disciplines, Inc., Chicago
This article may be circulated freely as long as the copyright notice is included.

Background

Last month we noted the common practice when comparing two programming languages of presenting:

A couple of readers complained that our example was a trivial and flawed one that wouldn't occur in a serious application. So we now offer two more somewhat less trivial we hope, Java examples from the same source.1 Needless to say the Clojure versions are shorter and more understandable.

Example 1

  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;
  }      

We may deplore the absence of commentary, but that's not the point of the author's example. The purpose of that little routine is to determine whether the argument string is blank, i.e. consisting entirely of unprintable bytes. It draws upon a library module isWhiteSpace, which works for a single character but not for a string.

Now let's make the obvious corrections and improvements:

  1. We're not sure what the extra outer parentheses on the second if are for, but it seems safe to get rid of them.
  2. Noting that the value of isWhitespace is already boolean we drop the redundant comparison with false.2
  3. The second condition on the first if duplicates a special case of the for statement, and can therefore be omitted.
  4. The local variable strlen, used only once, just adds confusion, so we can get rid of it.

So the tidied up module is:

  public static boolean isBlank (String str) {
    if (str == null) {
      return true;
    }
    for (int i = 0; i < str.length(); ++i) {
        if (!Character.isWhitespace(str.charAt(i))) {
           return false;
        }
    }
    return true;
 }   

I'm uncomfortable with the check for a null object. Why on earth would a user program call this routine for a null (as opposed to an empty string) parameter? But that's a common Java idiom, so we'll leave it alone.

If you don't remember why we changed i++ to ++i, see this explanation

One final, somewhat more controversial simplification: I would eliminate the unnecessary brackets around single-statement blocks. The designers of C didn't find them necessary, and they sometimes cause more mistakes than they prevent. That yields this compact and reasonably understandable equivalent:

  public static boolean isBlank (String str) {
    if (str == null)  return true;

    for (int i = 0; i < str.length(); ++i) 
        if (!Character.isWhiteSpace(str.charAt(i)))   
            return false;
    return true;
  }   

But if you prefer all those unnecessary brackets or if your employer's standards require them, we won't think less of your programming skill. Of course, the function still needs commentary.

Example 2

In a later chapter, the author presents this ugly Java example before showing his elegant Clojure equivalent:

 // From Apache Commons Lang, http://commons.apache.org/lang/
  public static int indexOfAny(String str, char[] searchChars) {
     if (isEmpty(str) || ArrayUtils.isEmpty(searchChars)) {
         return -1;
     }
     for (int i = 0; i < str.length(); i++) {
         char ch = str.charAt(i);
         for (int j = 0; j < searchChars.length; j++) {
             if (searchChars[j] == ch) {
                   return i;
             }
         }
     }
     return -1;
  }      
For this one we leave the tidying up to interested readers. But since it, too, lacks essential commentary in the source code itself, here's Halloway's explanation of what it does:

indexOfAny walks str and reports the index of the first char that matches any char in searchChar, returning -1 if no match is found.

Depending on readers' interest we may examine this example in detail later.
Hint: The nested loop presents difficulties we didn't confront in example 1.

Conclusion

We don't disagree with Halloway's conclusion that the Clojure versions are more compact and more understandable. But we wonder why it's necessary to contrive (or copy) flawed Java code for the sake of comparison.


1—Stuart Halloway: Programming Clojure, 2009, The Pragmatic Bookshelf, ISBN 10-10934356-35-6, pp 2 & 50-51
2Any comparison with the boolean constants true or false should raise suspicion.

Return to articles on programming.
Return to IDI home page.

Last modified April 16, 2017