I tried Oracle's NVL-like guy (transmitted) in Java. If it is non-null, it will be displayed as it is, and if it is null, a specific character string will be displayed.
Below, commons-lang3-3.5 is used.
Sample1.java
String s = null;
String result = ObjectUtils.defaultIfNull(s, "Slimy");
System.out.println(result); //=>Slimy
If you know it, you can write it in one line. The code that actually comes up from the programmer is like this ↓.
Sample2.java
String s = null;
//Those who are still better
if (s == null) {
s = "Slimy";
}
System.out.println(s); //=>Slimy
If you carelessly like this ↓.
Sample3.java
String s = null;
//Please stop
if (s == null) {
System.out.println("Slimy");
} else {
System.out.println(s);
}
If this is an input value, about 100 samples like Sample3 will be mass-produced, and then it will be uncontrollable anymore. Who will review it? I want you to do your best and stop mass-producing fucking code ...
Recommended Posts