Run the code through 'lint' from the very beginning, and maintain
the code 'lint-free' throughout the development. The effort saved in
debugging sessions is 10 times the effort invested.
No source file should be longer than a few hundred lines.
No function should be longer than three screen pages.
All functions should be prefixed with a macro to indicate whether they
are private (called only from within the file) or public (called from
outside the file):
#define private static #define public public void initialize_program() { } private void add_region() { }
The order of functions within a file should be consistent, either
top-down or bottom up, i.e.:
public void function1() { void function2(); function2(); } private void function2() { void function3(); function3(); } private void function3() { void function4(); function4(); }
TAB characters should not appear in code because:
Eight spaces is too much for indentation. Use 2, 3, or 4 space
characters for indentation. Whatever is chosen, be consistent
in identing.
if( a == b ) { for( i = 0; i < 10; ++i ) { if( i == 0 ) { (void) printf( "Hello\n" ); } else { (void) printf( "again\n" ); } } }
Where do the braces {and }go? It does not matter, as long as you
are consistent.
if( a == b ) { (void) printf( "Hello\n" ); }OR
if( a == b ) { (void) printf( "Hello\n" ); }
Defined constants and macros should be all caps:
#define MAX_STRING_LEN 243 typedef char String[MAX_STRING_LEN]; #define ERROR( msg ) \ { \ (void) printf( stderr, \ "Error %s\n", \ msg ); \ abort(); \ }
Likewise, enumerated types should use all caps for the constants, and first character capitalized for the name of the type:
typedef struct { AM, FM, SHORTWAVE } Radio_types;
Global variables, if used, should have the first character capitalized:
extern int Gradient_threshold;
Locals and statics should be all lower case, with underscores as necessary:
int i, j, max_value, min_value;
Do not skimp on variable and function names, use point_index instead of p, use sort_voxels_by_intensity() instead of sort(). One exception is for complicated expressions.
All functions should have a type. Do not let it default to integer.
WRONG:
function() { }RIGHT:
void function1() { } int function2() { }
In order to make your code lint-able, use (void) in front of any
function invocation where you ignore the returned value:
(void) printf( "Hello.\n" );