7

Possible Duplicate:
How to avoid “!= null” statements in Java?

Share your thoughts..

Community
  • 1
  • 1
The Student
  • 27,520
  • 68
  • 161
  • 264

4 Answers4

20

A first answer is to always return empty Lists, Sets, Arrays instead of null for method returning this kind of objects. Item 43 of Effective Java second edition from Joshua Bloch

Manuel Selva
  • 18,554
  • 22
  • 89
  • 134
5

Take a look at the Null Object Pattern. The basic idea is you have a special version of your class that you can use instead of null.

This special version has fields set to default values that make sense in your code. It means that you never have null references, you just have a class that doesn't do much or returns default values when used.

Carl Manaster
  • 39,912
  • 17
  • 102
  • 155
Matt Warren
  • 10,279
  • 7
  • 48
  • 63
  • +1. Empty collections are almost a special case of this - you just don't need to write a class for them. But the pattern of handing around something that isn't null and behaves safely is the same. – Carl Manaster Jul 29 '10 at 14:39
  • 1
    Although good in some situations, I find it is fairly limited in real world usage. – Robin Jul 29 '10 at 14:42
  • @Robin, yeah it has some limits, but it does beat getting NullReferenceExceptions!! – Matt Warren Jul 29 '10 at 16:11
4

In my opinion, null checks are evil. They show that there is no contract that establishes whether obj may be null or not. The good alternative would be to write the code in such a way that obj is guaranteed never null. For example: if a getter must get a non-null obj, but cannot, it must throw an exception itself.

Vlad
  • 35,022
  • 6
  • 77
  • 199
  • 1
    On the other hand, for many contracts, `null` is an acceptable value. So you wouldn't want to eliminate ever checking for null, only eliminate the defensive need to check *everything*. – matt b Jul 29 '10 at 14:43
  • @matt: indeed. however the check for null can be done not before using the object, but immediately after getting it. – Vlad Jul 29 '10 at 15:28
  • exactly. If null checks are everywhere in the code, it shows that the code is poorly written. – Oliver Watkins May 12 '14 at 07:45
1

Yoda Conditions

if (CONST_VALUE.equals(obj)) { ... }
Community
  • 1
  • 1
Yuval Adam
  • 161,610
  • 92
  • 305
  • 395
  • 1
    While very effective, that does impair readability some. – Brian Knoblauch Jul 29 '10 at 14:36
  • Totally agree, readability is severely impaired. But this is an idiom which you should be familiar with, and not afraid to use on occasion. – Yuval Adam Jul 29 '10 at 14:40
  • it also hides potential null pointer exceptions which can be bad – Oliver Watkins May 12 '14 at 07:46
  • @OliverWatkins why? By using the equals(Object) method of the CONST_VALUE object, it will never throw a NPE (assuming that the CONST_VALUE is a, well, constant that's already been initialized) – ToVine Apr 25 '16 at 12:08
  • yes it they are constants it should be fine. But it reads badly : obj.equals(CONST) reads far better than CONST.equals(obj) – Oliver Watkins Apr 25 '16 at 13:41