[ << ] [ >> ]           [Top] [Contents] [Index] [ ? ]

10. Variables

local variables
Add an end-of-line comment for each local variable declaration. Loop counters, variables used habitually in the same way in every function in a module, etc., are exempt.

If there are several local variables in a function, put them into logical groups, with a blank line between groups. Each group should be preceded by a comment line explaining their purpose as a group.

If some variables are relevant only if another variable is set to a particular value, etc., indicate that as part of a comment.

(These are the same as the rules that apply to structures.)

no alignment
Do not align variable names, structure member names, etc., in declarations. It takes too much maintenance effort and isn't always possible.

pointer declarators
Pointer declarators bind to the variable name, not the type name. Write int *x, not int* x.

Limit variable scopes, but not obsessively. Don't be afraid to use nested blocks to do so.

Avoid global variables, except read-only and write-once, read-many variables.

Avoid static buffers and fixed-size buffers for data of indefinite length. Instead of returning a pointer to a function-local static buffer, use a parameter with a pointer to a buffer to stuff the result into.

[ << ] [ >> ]           [Top] [Contents] [Index] [ ? ]

This document was generated by Ben Pfaff on September, 11 2001 using texi2html