Monday, February 9, 2009

Another thought about compiler null checking: A method signature is a contract between the method provider and the method consumer, but it doesn't say anything about nulls. If the method provider and method consumer have no other means of communication, they both have to check for nulls. The consumer should make sure the method arguments are not null before calling the method and the provider has to check for nulls before using them since neither of them can trust the other. Of course the provider can just say that a NullPointerException is going to be thrown should the consumer be so stupid as to pass a null, and that's what is often done. But that is a run-time bug just waiting to hide in the bushes, avoiding unit tests and the QA tests and the UA tests, and spring out years down the road, when the software is in production and the development staff is long gone. No, I think what's really needed is for the Java language to add the NOTNULL qualifier to method arguments and enforce it. The compiler can know whether an argument could be null.

No comments: