Choose Your Poison

When dealing with programs there are almost always multiple ways to write same thing. Rule of thumb is usually to write it in most readable way. And there people start disagreeing what exactly is readable. Let's take simple condition:

if (object.equals(obj1, obj2)) {
...
}
if (object.equals(obj1, obj2) == true) {
...
}

Both these statements do exactly same thing. Heck, they even get compiled to same code. However most programmers I have ever met prefer first one. It just makes intention clear in shorter form.

It's negative cousin would be:

if (!object.equals(obj1, obj2)) {
...
}
if (object.equals(obj1, obj2) == false) {
...
}

Again, both statements are same. But I can bet that they will do almost perfect split among programmers. Half of them will prefer concise form with other half preferring longer form. Reasons will vary. For some exclamation mark is too easy to omit when scanning code. For some longer form just sticks out for no good reason.

Personally I passionately hate negative statements and I will avoid them whenever I can. If I really need them I am in group that strongly believes that later form, while longer, is superior as readability goes. For me that is worth seven additional characters. Strangely enough, in positive cases, I omit true.

P.S. One more variation of negative case would be:

if (!(object.equals(obj1, obj2))) {
...
}

It might as well be in goldilocks zone. It gives a bit more visibility to exclamation mark by enclosing everything in another set of brackets. Of course, given statement long enough, it can look rather crowded.

4 thoughts to “Choose Your Poison”

  1. Usually I like to make any code as compact as possible, so may start out long for developement, then condense to reduce code bytes.
    My web pages, as an example, have very little html code in them, as they are generated by the Javascripts.
    As the scripts are condensed, and generate the redundant html tags, the pages DL faster, and the js engines, once cached are even faster.

    BTW: I am here because of VHD attach. Your first version was very good. Your later versions have only gotten better! Very Well Done.

    Now if there was a way to attach a vhd, before an OS system boot that would be nice.
    Ex. In Win7 Pro or Ultimate, you can boot into a slected .vhd; but if you could mount the vhd before the selection, then you could run any windows OS version, like the way virtual machines run {vmWare}

    Computer Majic:
    Skype: Clearline_org

  2. For positive conditions, I consider adding “== true” unnecessary and n00bish – the expression evaluates to a boolean, and the execution continues if that value is true, and code can be read more like a sentence in a human language (compare “if object A equals object B” vs “if object A equals object B is true”).

    For negative conditions, first of all, I try to use positive condition instead, if possible. If not, I like to warn the reader that it’s a negative condition by visually separating the exclamation mark from the rest of the expression, e.g. if( ! a.equals(b)).

    1. +1, having policy to put positive condition first increases readability of the code.

      BTW Here is one awful example that I saw once:

      boolean a;
      ...
      boolean b;
      ...
      if (a == true) {
      b = false;
      } else {
      b = true;
      }

      Try to count how many necessary checks there is!

Leave a Reply to Clearline Cancel reply

Your email address will not be published. Required fields are marked *