null parameters considered less harmful

Mike Feathers has posted an article in which he defends the use of null parameters, but only under certain circumstances: He likes them as parameters to constructors when building lightweight objects in tests.

As you know by now, dear reader, I have railed long and loud against the use of null parameters. And so I find myself disagreeing with Mike on this topic. [Mike, I would have commented directly on your blog, but why do I need an Artima account to do so?]

Mike suggests that it is always worth trying a line such as

ADBDocument doc = new ADBDocument(null, 0, null);

when writing a test. If it works, he says, you get a lightweight object that can be just good enough to write the test and continue. But is the code now truly intention-revealing? If it were indeed sensible or feasible to create a ADBDocument with no inputs to the constructor, perhaps a zero-argument constructor should have been provided; or a factory method called newUnprintableDocument()? It seems to me that there is now a brittle relationship between the test method and the implementation of at least one constructor for the ADBDocument class, and that makes me nervous.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s