Go to the first, previous, next, last section, table of contents.


5.4 Type Declarations

Whenever possible, arguments passed by reference (ie., pointers) should also be declared const. Not only does this improve the clarity and safety of your code, but it allows other programmers to use your code. A procedure argument that is not declared const can never be const in any code that uses that procedure.

Do not mix declarations and initializations. That is, do not combine the type declaration of automatic variables with their initialization in the body of a procedure. For example, replace int i=0; with the declaration int i; in the preamble followed by the actual initialization i=0; in the body of the procedure.

Declare variables in the narrowest possible scope. If you only use a variable inside one of the branches of a conditional statement, then it should be declared only in that branch. This reduces the risk of improperly initialized variables and inadvertent name conflicts. It also makes the code easier to understand.

In general, typedefs should be to the object itself, rather than to a pointer to the object. This makes memory management easier in most cases, and improves clarity because the caller better understands the semantics of procedure arguments.

Objects requiring more space than two machine words should typically be passed by reference for the purposes of efficiency. However, objects that are used solely to return multiple values from a procedure call should typically be returned by value in order to simplify memory management.

If at all possible, modules should be parameterized by type. Type parameterization allows you to design efficient, reusable modules. In C, types are parameterized by the size of their objects (in bytes) along with a minimal set of procedures necessary to operate on those objects.

5.5 Additional Guidelines


Go to the first, previous, next, last section, table of contents.