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.
Doing this is worse than just doing a simple null check, because it's more verbose and you're new'ing up a wrapper object just to immediately unwrap it again. The Optional class has a whole declarative api that you can safely code against, transforming the value within the pipeline and providing default values:
Yes, it's a really blatant example, but just to belabor the point, I'd go so far as to say if you're calling Optional.get() at all, you're probably doing it wrong.
You probably don't ever want to be referencing an Optional object in your calling method. In other words, you shouldn't really ever need to instantiate a variable of type Optional:
Optional<List<Customer>> optCustomers = // Stop here! Don't do this!
customerApi.getAllCustomersByState("CA");
List<String> customerIds = new ArrayList();
if (optCustomers.isPresent()) {
List<Customer> custList = optCustomers.get();
for (Customer cust : custList) {
customerIds.add(cust.getId());
}
}
return customerIds;
Not only is this forcing the declarative Optional api into an imperative pattern, it also ignores the declarative capabilities of the Collection api. Instead:
There's almost always a way to do what you want to the returned value within the Optional api declaratively using operators, then work with the result. Not saying you don't already know all this, but elaborating for the sake of discussion.
Can someone explain to me why Optional is considered bad practice as method parameter? I mean, sure, ideally you should not use it this way, but sometimes, something just can be null, but different methods may want to handle it differently and Optional declared that possibility clearly and forces you to handle the case (or ignore it at your own peril). Point is, I like Optional. It has legitimate uses as parameter.
I agree, as with many things it depends on the use case. The way I see it, Optional parameter declares in no uncertain way that passed wrapped argument can be null and the method knows it. That does not mean I use it often, or at all really, I can think or two uses in couple of years. But some people seem to be religiously opposed to it.
That's a pretty unlikely mistake to make, and it will throw an NPE the first (and every) time it runs. If you accidentally pass null for an Optional, you didn't do it conditionally. You'd be doing it every time.
It's good for APIs and interfaces because it lets the implemented and caller know that something is considered optional. Most people don't like continually wrapping their inputs into an optional even though it's pretty easy. Just checking for null is far more common practice and serves nearly the exact same purpose. It's a matter of personal preference.
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.