Analyzing a single, trivial Java statement

Java isn't a bad language, but it is also not great, and it's age shows.

Let's look at this simple Java statement:

if (!Boolean.TRUE.equals(getIsRequired())) {

It's very simple and common, and yet it includes a lot of the things I don't particularly like about Java:

  • We need a getter for a trivial property access. I like the Python way with @property, or the Kotlin way with getters and setters being called implicitly.
  • It is not explicit that getIsRequired is a method. Admittedly, every function in Java is method, so it's fairly clear. Still, I again prefer Python's explicit way: include self/this.
  • equals is used because == compares object identity. I think just about every language does this better, but I particularly like the Rust way, where objects can be made comparable but are not by default (which is caught at compile-time).
  • Boolean.TRUE.equals is used, because getIsRequired could be null, since there are no non-nullable types. Many languages have solved this, Kotlin being one of the popular ones that's compatible with Java.
  • It uses both (parentheses) and {braces}. I think this is necessary because an if condition can be followed by a single statement without braces. I also think that's a stupid possibility that should not exist, so the parentheses should be useless.
  • It uses ! instead of a not keyword, which I personally think would have been more readable (same for && and ||).

So how would I prefer it to look? One of these:

if not this.required {
if not this.required:

where this.required can only be true or false, not null.


Topics: #coding #discussion #programming-languages #java


No comments yet

You need to be logged in to comment.