John Chamberlain
Developer Diary
 Developer Diary · You Heard It Here First · Friday 27 February 2004
Principles of Programming: Make Precedence Explicit
When crafting expressions it's a good idea to make precedence explicit. Remembering whether '>' comes before '<<' or '||' is difficult if not for you, then for someone who might be reading your code. I find adding parentheses to make things explicit not only makes the code more readable but reduces the chance of bugs.

The problem with bugs in expressions is that they are often silent. Sometimes a mistake in a expression if used in an if-statement, for example, may trigger an "incompatible type" error, but just as often a bad expression will return a usable value--which is not the one you want. Since this is an error in data logic rather than program inconsistency that would cause a crash the bug will more easily go undiscovered. This is especially true if you are working in C in which there are fewer type checks at compile time.

Adding the parentheses may seem redundant but in the long run it will pay off.

return to John Chamberlain's home · diary index
Developer Diary · about · info@johnchamberlain.com · bio · Revised 27 February 2004 · Pure Content