Sunday, August 05, 2007

Null-Parameter und Rückgabe von null in Java

Ein guter Artikel über "null" als akzeptierter Methodenparameter und den Rückgabewert "null" in Java:

I am not a fan of methods that accept null and I can find very few reasons for ever wanting to return null. Once a null gets into your code it can cause havoc, which is best discovered as soon as possible during development as it either indicates a serious problem or a buggy method returning null under certain circumstances (usually instead of an empty Collection).

How many times have you had to rewrite a simple equals call to allow for a null?

Etwas arg philosopisch und in der Praxis nicht einzuhalten, aber trotzdem ein guter Rat. Auch sollte man das Null-Muster (ps) in diesem Zusammenhang nicht vergessen.

Ich hab immer die Regel des Eclipseprojektes gemocht. "Null" ist dort - wenn ich mich richtig erinnere - als Eingabeparameter ungültig, es sei denn der Fall wurde in den Javadoc-Kommentaren explizit erlaubt.

No comments:

Post a Comment