Fantastic Java API and Where to Find Them: Objects.toString
Do you have stuff like that in your code?
string = string == null ? “” : string;
or like this one…
if(string==null) string="";
or, more smart, did you create a method like this?
String ifNullThenEmpty(String string) {
return string == null ? "" : string;
}
You can put everything on Utilities class as a static method …
…Don’t worry, I did it too, this is not a code crime against Alan Turing’s memory, James Gosling won’t hate you.
However, if you use a version of Java≥ 7, why do you reinvent the wheel?
Oracle has already done it for you! Use it!
Class java.util.Objects
contains a method with this segnature
String toString(Object o, String nullDefault)
An example is…
string = Objects.toString(string,””);
or, more simple, with static import
string = toString(string,””);
Simple, clean, and you don’t test it. Oracle has already tested it for you.
You can find this post and others on my blog: trueprogramming.com