[ Content | View menu ]

Self-Confident Code

Mark Mzyk | November 9, 2008

Pervasive null tests are a code smell.

– Avdi Grimm

I agree.

If you’re often checking for null, then you don’t trust your code.  Why don’t you trust it?  Instead of checking for null, why don’t you fix the failure point that is causing the null?

Null checks violate the fail fast, fail often principle.  If it’s null and it’s not supposed to be, fail.

Null checks are broken windows.  Something is wrong with your code and it should be fixed now instead of being left to fester.  The null checks are telling you this.

To once again echo Avdi:

Make your code self-confident. Eliminate null checks wherever you can.