I have a lot of code like this:
if( null == myString || "".equals(myString))
doAThing();
Enter fullscreen mode
Exit fullscr...
For further actions, you may consider blocking this person and/or reporting abuse
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
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 a new project - this is for old 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