Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

The Optional Abomination

DZone's Guide to

The Optional Abomination

Programmers should look to reduce complexity whenever they can. Bringing Optional into the equation, among other choices, is a recipe for headaches.

· Java Zone
Free Resource

The single app analytics solutions to take your web and mobile apps to the next level.  Try today!  Brought to you in partnership with CA Technologies

There’s this piece of code… I keep thinking about it. It shows up in my dreams. I open it every time I get to work. I learned it by heart already. It scares me. It reminds me of every bad thing that ever happened to me in my life. It reminds me of every line of crappy code that I ever wrote. It’s like a whisper in my ear: You can’t avoid me. Help! Please help!

public class PresenceCheck {
    public static boolean isPresent(Object val) {
        return Optional.ofNullable(val).isPresent();
    }
    public static boolean isNotPresent(Object val) {
        return ! isPresent(val);
    }
}


My eyes are burning! Aaarrgh! Why? Why? Why? Why? Why?!

To be honest, I knew things are going the wrong way when Java 8 came along and I saw people using Objects.isNull and Objects.nonNull instead of a normal null check, but this?!

A big part of our profession is handling complexity. We take complex domains and make software out of it. We hide the complexity between a (supposedly) nice UI, so that the end-user does not have to deal with it. Ideally, what he sees is a reflection of his mental model of the problem and he feels “at home” using our software.

I’ve made the words “complex domains” bold for a reason. The world that we’re trying to handle is complex already. This complexity needs to be then represented in the code somehow, in order to solve our user’s problem. That’s what we call inherent complexity.

Unfortunately, more often than not, there’s another kind of complexity lurking in there — accidental complexity. This is the kind of complexity that we developers create “accidentally” when coding and it’s not related to complexity of the domain itself — it’s not necessary to make the system work.

Inherent complexity mixed with accidental complexity is an extremely dangerous mixture. Drink it for too long and your system will become a legacy that you don’t want to drag along. Therefore, we have to do everything we can to limit the complexity. We want simplicity!

Now, it’s beyond a single post, probably beyond a single book and maybe even beyond a single mind to understand and explain all the ways to limit complexity in software. But whatever programming language you use, whatever framework stack you choose and whatever kind of applications you make, the first step is always the same: LEARN YOUR GOD DAMN PROGRAMMING LANGUAGE!

Read books, documentation, blogs, read the damn source code. Sometimes, it’s a matter of one click in the IDE to avoid making a fool out of yourself:

/**
 * Return {@code true} if there is a value present, otherwise {@code false}.
 *
 * @return {@code true} if there is a value present, otherwise {@code false}
 */
public boolean isPresent() {
    return value != null;
}


And you, Optional Abomination, leave me and my life, now!

Image title

CA App Experience Analytics, a whole new level of visibility. Learn more. Brought to you in partnership with CA Technologies.

Topics:
java ,optional ,complex code

Published at DZone with permission of Grzegorz Ziemoński, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}