Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Finding Null or Empty String Checks in Java
Gunnar Gissel
Gunnar Gissel

Posted on • Edited on • Originally published atgunnargissel.com

     

Finding Null or Empty String Checks in Java

I have a lot of code like this:

if( null == myString || "".equals(myString))    doAThing();
Enter fullscreen modeExit fullscreen mode

The codes fine, really. Very idiomatic, gets the job done.

Lately, I've been on a predicate kick. I like replacing my conditionals with predicates that describe the intent of the conditional statement. I can also replace a lot of my boilerplate code with a couple common predicates. This is one I use a lot:

 public static final Predicate<String> NULL_OR_EMPTY = (in) -> null==in || "".equals(in);
Enter fullscreen modeExit fullscreen mode

It's true; if the code base had been crafted with Optionals or other null-avoidance techniques, that I could mostly ignore this check. My code base has history and weight and external interfaces, so there are plenty of this check and others like it.

I have been usingthis regex to find likely places that I can apply my predicate:

  /\(\s*null\s*==\s*(\S*)\s*\|\|\s*""\.equals\(\s*\1\s*\)\s*\)|\(\s*(\S*)\s*==\s*null\s*\|\|\s*""\.equals\(\s*\2\s*\)\s*\)|\(\s*null\s*==\s*(\S*)\s*\|\|\s*\3\.equals\(\s*""\s*\)\s*\)|\(\s*(\S*)\s*==\s*null\s*\|\|\s*\4\.equals\(\s*""\s*\)\s*\)|\(\s*""\.equals\(\s*(\S*)\s*\)\s*\|\|\s*null\s*==\s*\5\s*\)|\(\s*(\S*)\.equals\(\s*""\s*\)\s*\|\|\s*null\s*==\s*\6\s*\)|\(\s*""\.equals\(\s*(\S*)\s*\)\s*\|\|\s\7\s*==\s*null\s*\)|\(\s*(\S*).equals\(\s*""\s*\)\s*\|\|\s*\8\s*==\s*null\s*\)|\(\s*null\s*==\s*(\S*)\s*\|\|\s*""\s*==\s*\9\s*\)|\(\s*(\S*)\s*==\s*null\s*\|\|\s*""\s*==\s*\10\s*\)|\(\s*null\s*==\s*(\S*)\s*\|\|\s*\11\s*==\s*""\s*\)|\(\s*(\S*)\s*==\s*null\s*\|\|\s*\12\s*==\s*""\s*\)|\(\s*""\s*==\s*(\S*)\s*\|\|\s*null\s*==\s*\13\s*\)|\(\s*(\S*)\s*==\s*""\s*\|\|\s*null\s*==\s*\14\s*\)|\(\s*""\s*==\s*(\S*)\s*\|\|\s*\15\s*==\s*null\s*\)|\(\s*(\S*)\s*==\s*""\s*\|\|\s*\16\s*==\s*null\s*\)/g
Enter fullscreen modeExit fullscreen mode

It's an ugly one, but it matches a lot of common patterns for checking if a string is null or empty. I'm hopeful that folks will suggest either a better way of finding null or empty checks or help me add to my hideous regex!

Get a monthly email with great tech and tech leadership articles from around the web

Thanks to Jan Tik for the header image

Follow me at my blog!

Top comments(2)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
niharmore33 profile image
nihar
  • Joined

You should leverage StringUtils.isEmpty(str), which checks for empty strings and handles null gracefully.
Learn Java here:hackr.io/tutorials/learn-java

Example:

System.out.println(StringUtils.isEmpty("")); // true
System.out.println(StringUtils.isEmpty(null)); // true
Google Guava also provides a similar, probably easier-to-read method: Strings.isNullOrEmpty(str).

Example:

System.out.println(Strings.isNullOrEmpty("")); // true
System.out.println(Strings.isNullOrEmpty(null)); // true

CollapseExpand
 
monknomo profile image
Gunnar Gissel
Saving fish by writing code! Applications developer in fisheries, specializing in webapps and moving 'enterprise-y' legacy systems to modern agile systems - Email or tweet me if you want to talk!
  • Email
  • Location
    Alaska
  • Education
    Bachelor's in Physics
  • Work
    Application Developer at NOAA
  • Joined

That's in Apache Commons, right? It's a very useful library, and if I have it in a project already, I'm going to use it, but if I don't already have it, I won't. Guava is great too, and starting from scratch I generally toss it in.

In this particularly case I'm dealing with a large established code base that is politically resistant to new libraries. Being that it is established, it has many different patterns from different eras of development. The regex is part of an effort to track down some of those patterns and unify them, without adding a library. I think it is pretty obvious that you wouldn't use a regex to find a pattern of coding in anew project - this is forold projects that may not have always done it the right way.

I would be interested if you know of a better way to find more null or empty checks in strings than just adding to my regex when I find a new pattern

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Saving fish by writing code! Applications developer in fisheries, specializing in webapps and moving 'enterprise-y' legacy systems to modern agile systems - Email or tweet me if you want to talk!
  • Location
    Alaska
  • Education
    Bachelor's in Physics
  • Work
    Application Developer at NOAA
  • Joined

More fromGunnar Gissel

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp