Improve code readability by putting spaces around the “not” symbol

By Adrian Smith16 Aug 2018300 words1 mins to read

When I first started working, I noticed my boss at the time had a coding convention that I'd not seen elsewhere. As I was new to work, and the convention seemed logical, I adopted it, thinking it might be standard in the world of work. Alas I was wrong, I've never seen anyone else use it. But I still think it's a good convention.

And that's based around the following fact. The C family of languages use the exclamation mark "!" as the symbol for "not", and that's actually a visually small and easy-to-oversee symbol.

In a long line such as:

if (!myObject.equals(anotherObject)) {

The exclamation mark is easy to oversee. So my now-ex-boss would always put a space before and afterwards to emphasize it.

if ( ! myObject.myMethod(param1, param2)) {

Some syntax like the end-of-statement ";" are mandatory and thus if one oversees it it's not a big deal (if it's really not there, it's a compile error, fix it, move on). The "not" symbol is not like that: there's no reason to assume it's there, unless you've seen it's there. So it's important not to oversee it.

Code readability is important and I believe that putting spaces around the "not" symbol enhances that.

This article was written by Adrian Smith on 16 Aug 2018

Follow me: Facebook | Twitter | LinkedIn | Email

More on: Coding