What is the best way to avoid the set of "if (obj! = Null)" in Java code?

Possible duplicate:
How to avoid the "! = Null" statements in Java?

Share your thoughts ..

+3
source share
4 answers

The first answer is to always return empty lists, sets, arrays instead of zero for a method that returns this kind of object. Paragraph 43 of Effective Java Edition by Joshua Bloch

+18
source

See the Zero Object Template . The basic idea is that you have a special version of your class that you can use instead of null.

, , . , , , .

+5

In my opinion, null checks are evil. They show that there is no contract that establishes whether it can objbe nullor not. A good alternative would be to write code in a way that is objnever guaranteed null. For example: if getter should not receive null obj, but cannot, he must throw an exception.

+4
source

Yoda Conditions

if (CONST_VALUE.equals(obj)) { ... }
+1
source

Source: https://habr.com/ru/post/1757157/


All Articles