You don't need to make an interface for everything. Everything doesn't have to be private. Write code that doesn't allow for or tolerate nulls and stop checking for them all the time.
Write code that doesn't allow for or tolerate nulls
Not sure I agree with the "tolerate nulls" part of that. You should always be flexible with what you accept. In Java nulls can happen so in reality you need to be ready for them.
Nullity is something of a lost cause, but you should strive to make invalid states unrepresentable in the first place and, failing that, should be explicit about your preconditions and fail ASAP if they're violated. Being excessively lenient in what you accept as input is a good way to build up a dodgy pile of inconsistent garbage.
Oh, you have not lived through the age of Web browser wars then.
One of the worst use cases for the principle of “be flexible in what you accept” was how early web browsers implemented html parsing.
It was full of weird rules of how to parse broken html and each browser has subtly different way to make the broken input display a web page.
And to make it worse, the whole web was full of markup taking advantage of those nonstandard quirks and idiosyncrasies.
Those quirks are at least partially to blame for why it is so hard to implement a new web browser from scratch these days. For the web has grown to the point where it is impossible to fix the pages themselves. You need to make your browser bend three ways backwards instead to accept and render those cases that every other browser has learned to accept.
I would not be surprised to find that “being lenient in your input” has more excessive cost attached to it than having null in your type system.
117
u/halfanothersdozen Apr 14 '23
You don't need to make an interface for everything. Everything doesn't have to be private. Write code that doesn't allow for or tolerate nulls and stop checking for them all the time.