Thursday, 26 May 2011

It's obvious - not

One of my pet hates is when people write code that is technically correct, but easily mis-understood.
I came across the following today that really grates me:

if (!(a <= 0 && a >= -2))
{ ...}

If you're great at boolean logic, then that's fine - but most people read it incorrectly and mis-interpret the meaning. It would be much more obvious to write it like this:

if (a < -2 || a > 0))
{ ...}

It says the same thing, but is easier to understand

No comments:

Post a Comment