Wednesday, February 16, 2011

To make it just that little bit more confusing...

Some people seem to have developed a habit of reversing comparisons to keep the Constant at the front.

So

If (itemCount > 0) {...  becomes If (0 < itemCount) {...


I think this stems from changing

If (itemCount== 0) {... to this If (0 == itemCount) {...

To avoid dropping an "=" and ending up with  this.

If (itemCount = 0) {...

Of course most modern compilers will warn you about the assignment in the if statement, and you can (and probably should) set warnings to be treated as errors.

Which means there's really no call for code that looks like this

If (! 0 < itemCount) {...}

Where you also invert the condition to reduce nesting of if statements in your code.

The compiler will consistently read the code regardless of pretty formatting, nice variable names, and clear and concise expression of ideas. Humans are not compilers. Write your code for the Human audience. The compiler will cope regardless. It almost always does.

No comments:

Post a Comment