6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

JavaでNVLっぽいやつ

6
Posted at

OracleのNVLっぽいやつ(伝われ)をJavaでやってみた。
null以外だったらそのまま、nullだったら特定の文字列が出る。

以下、commons-lang3-3.5を使用。

Sample1.java
String s = null;

String result = ObjectUtils.defaultIfNull(s, "ぬる");
System.out.println(result); //=> ぬる

と、知っていれば1行で書けるものを。
実際にプログラマから上がってくるコードはこんなの↓とか。

Sample2.java
String s = null;

// まだマシな方
if (s == null) {
    s = "ぬる";
}
System.out.println(s); //=> ぬる

うっかりするとこんなの↓とか。

Sample3.java
String s = null;

// やめてくださいしんでしまいます
if (s == null) {
    System.out.println("ぬる");
} else {
    System.out.println(s);
}

これが入力値とかだったりすると、Sample3みたいなのが100個くらい量産されてしまって、そうなるともはや手に負えない。誰がレビューするんだよ。
頑張ってクソコードを量産するのは止めてほしいなあ…

6
6
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?