Transcript
Page 1: Better Strategies for Null Handling in Java

Better Strategies for Null Handling in Java

Stephan Schmidt

Team manager PMI-3

Berlin, 05.08.2008

Page 2: Better Strategies for Null Handling in Java

2

Most problematic errors in Java

� 2 runtime problems in Java

ClassCastException

„Solved“ with Generics

NullPointerException (NPE)

Solution?

Page 3: Better Strategies for Null Handling in Java

3

Problems with NPEs

– RunTime Exception

– Point of NPE easy to find

=> But not clear where the NULL value comes

from

Page 4: Better Strategies for Null Handling in Java

4

Handling of NULL Values

– Easy to forget

– No support from type system

– No tracking of NULL values

• Can a reference be NULL ?

Check before

if (map.containsKey("Hello")) {

String name = map.get(“hallo”);

} else { … }

Check after

String name = map.get("Hello");

if (name != null) {

...

} else { … }

Page 5: Better Strategies for Null Handling in Java

5

Null Handling in Groovy

�Safe Navigation Operator ?.

user, address can be NULL

will simply return NULL instead of throwing an exception

def user = users[“hello”]

def streetname = user?.address?.street

Page 6: Better Strategies for Null Handling in Java

6

Null types in Nice language

Nice language NULL types

- ?String name => possibly NULL

- String name => not NULL

String name = null;

=> Compiler error

Page 7: Better Strategies for Null Handling in Java

7

NULL Handling with Annotations

�@NotNull, @Nullable in Java

IDEA and others, JSR 308

Automatic checks for NULL

IDEA tells you when NPEs will occure

@NotNull

public String get(@NotNull String name) { … }

Everything not null and @Optional for NULL better solution

Page 8: Better Strategies for Null Handling in Java

8

Scala Option Class

map.get("Hello") match {

case Some(name) => // do something with name

case None => // do nothing

}

Option can have a value or not (think container with 0 or 1 elements).

Subclasses are Some and None

Must deal with None (NULL) value, cannot ignore

Called Maybe (Just, Nothing) in Haskell

Page 9: Better Strategies for Null Handling in Java

9

Option in Java

Option<String> option = map.get(„hello“);

if (option instanceof Some) {

String name = ((Some) option).value();

….

} else {

// option is none, there is no „hello“

}

�Explicit handling of „NULL“ value necessary

�Or:

�option.isSome() and option.value() without cast

Page 10: Better Strategies for Null Handling in Java

10

For Trick for Option with Iterable

public class Option<T> implements Iterable<T> { … }

for (String name: getName(“hello”)) {

// do something with name

}

Sometimes the none case needs no handling

For and Iterable<T> can be used

For automatically unwraps Option, does nothing in None case

None returns EMPTY list, Some one element list with option value

Page 11: Better Strategies for Null Handling in Java

11

Convenience methods

Option<String> name = none();

Option<String> name = option(dontKnow);

Option<String> name = some(„stephan“);

Page 12: Better Strategies for Null Handling in Java

12

How does this method handle NULL values?

�API makes the intention clear

� public Option<String> getName() {…}

� public String getName() { …}

� public void setName(Option<String> name) { … }

� public void setName(String name) { … }

Page 13: Better Strategies for Null Handling in Java

13

Easy default values with orElse()

�Easy handling of default values

�Very little code compared to Check Before or

Check After for default handling in Java

String name = map.get(„hello“).orElse(„stephan“);

Page 14: Better Strategies for Null Handling in Java

www.ImmobilienScout24.de

Questions?