if (condition)
{
statements;
}
or
if (condition) {
statements;
}
The only justification I've ever seen for the first form (I prefer the second, more compact form) is that the braces for each level left-align. I never trust alignment when I am looking at someone else's code. I use an editor that supports "find matching bracket".
C and C++ support omitting the brace brackets if a simple (rather than compound) statement follows the construct. I advise against using this form. ALWAYS use brace brackets even for constructs that have only a simple statement. It avoids ambiguity and if you need to add another statement you don't need to add the braces.
Always write your comments first (a header in each file that includes a description AND an audit trail). The reason you need to write your comments first is to force you to think about what the code should be doing (instead of how). After all, if you don't know what the code should do then you shouldn't be writing it.
White space (but not too much). Code should be pleasing to the eye as well as functional. If you took a great work of literature and removed all of the white space, it would still be great, but much more difficult to read. Same with code.
Break up your code into small, functional modules. I had to modify a REXX program written by a co-worker. It was your basic "stream-of-consciousness" code. One monolithic block (no whitespace - no indentation). Also, all variables were of the x, xx, iii, j1, j2, etc type (before you ask - he wasn't an engineer). It was easier to just rewrite the mess.
Don't get clever. A piece of code that is simple and clear is almost always better than code that uses clever (and usually obscure) tricks. If the trick provides massive savings in time or resources then use it. But make sure you thoroughly document the trick.
Don't patch bad code - rewrite it.
When reading someone else's code, don't assume anything. I once spent hours trying to find a bug in a thousand-line piece of FORTRAN code (on a system that did not have a source level debugger). The problem ended up being in an include file in which the constant NINE was initialized to the value 5.
This post has been edited by rjdegraff: 18 February 2009 - 07:50 AM

New Topic/Question
Reply




MultiQuote










|