Dec 11, 2012

Mind the gap: String.isEmpty()

Articles labeled "Mind the gap" are short articles mostly about simple problems that arise from using different API levels of Android. They are more short trivia postings than big teachings about Android development.

I try to write code as readable as possible. That's the reason why I don't want to compare a String to an other empty String object or check its length when I want to know if a String is empty.


In the book "Clean code" by Robert C. Martin you can read Grady Booch saying: "Clean code reads like well-written prose". So I try to use String.isEmpty() for that reason. Internally it may do a length check as well (I stopped my investigation at the native keyword) but when reading the following snippet it is absolutely obvious what I intend to do.


Even though isEmpty() has been introduced in Java 1.6 it hasn't been available in Android until API level 9 (Android 2.3) so I accidentally caused some crashes on earlier versions of Android. Nowadays lint thankfully saves me from doing this error.

So what to do now? I don't know if someone at Google felt the same but there is a class in the Android framework that solves that problem: TextUtils. This class also has a lot of other helpful methods like join() to join an array of elements to a String using a delimiter or getReverse() to reverse a String (Take that interview question!).

In addition to that the TextUtils class has a method isEmpty() that is available since API level 1.


Phew! By the way: Internally isEmpty() checks if the length of the String is 0 (and does a null check).

5 comments:

  1. One small thing to note :
    TextUtils.isEmpty(value) works with a null String and returns false.
    String.isEmpty() will throw a NullPointerException is the String is null.

    So the behavior of these 2 methods are not 100% the same :)

    ReplyDelete
  2. Great tips. I knew neither about StringUtils.isEmpty() nor about TextUtils. I've been coding my own StringUtils.isNullOrEmpty(String aString) method all this while!

    Off-topic - I'm curious as to how you create such great screenshots of the code - I mean, isn't it tedious? Also, why have you chosen to present them as screenshots instead of inline code using HTML code elements or tools like prettify.js? The latter makes it easy for craftsmen skilled in Ctrl-C + Ctrl-V :-)

    ReplyDelete
  3. @Foxykeep I think TextUtils returns true if the string is null.

    ReplyDelete
  4. Yep, it returns true. See the implementation here: http://bit.ly/VTZdBb

    @Kiran: Sure, creating these screenshots is annoying. I tried different JavaScript libraries at the beginning but haven't been happy with them. Most of the time the library wasn't the problem but the blogger posting editor - it's still doing strange things. So at the end using screenshots was easier than fighting with the editor. ;)

    For copy&paste I normally link to a Gist[1] or to the "Android Zeitgeist Samples"[2] repository on GitHub. For the one-liners here I didn't do that. ;)

    [1] https://gist.github.com/pocmo
    [2] https://github.com/pocmo/Android-Zeitgeist-Samples

    ReplyDelete