Blog post

Don’t be anti-negative

Dattatray Kale

-
August 12, 2019
Clean Code
Software Design
Software Craftsmanship
Programming

The negative conditionals in code blocks are most of the time difficult to read and understand.

It becomes worse when you make the conditionals anti-negative.

Photo by Aarón Blanco Tejedor on Unsplash

Before

Try to read above code snippets and try to understand it. Please note the highlighted parts. Does that code read like

  • When inverse of a package is not damaged then report an error: “Package is damaged”.
  • When inverse of a package is not empty then report another error:
    “Package is empty”

Ouch! Unreadable? Complex? Painful, isn’t it? Let’s revisit the same condition with positive conditionals.

After

Another example:

Before:

Above snippet is showing hours logged for the day if it’s not a non-working day!

After

Now, this reads like a simple statement. Show hours logged if its working day.

Originally published at https://beingcraftsman.com on August 6, 2018.

Related Blog Posts