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.