Wireless Systems Group
C Coding Standards Quick Reference
Comments 3.1 |
Every file that contains source code shall be documented with an introductory comment that provides information on the file name, its contents, and copyright information. |
Every file that contains source code shall contain a revision history. |
Files containing more that one function shall contain a function header for each function describing the function. |
Code shall not be commented out. |
Comments shall not be nested. |
Constants 3.2 |
Constants shall be used instead of hard coded numbers. |
All locally defined constants shall be defined before the first function in the file. |
If TRUE and FALSE are to be defined, TRUE shall be 1 and FALSE shall be 0. |
NULL shall only be used for pointer comparisons and initializations. |
Expressions 3.3 |
Implicit logic tests shall be avoided. |
All conditional expressions shall be explicit. |
Float values shall not be tested for equality. |
When checking a value for equality to a constant value, the constant should be on the left-hand side of the expression. |
Unsigned variables shall be used for all bit-manipulation operations. |
Flow Control Statements 3.4 |
The code following a case label shall be terminated by a break statement. |
A ‘switch’ statement shall contain a ‘default’ branch that handles unexpected cases. |
A “goto” statement shall never be used. |
A null statement as body of a loop shall include a comment to make the intent clear. |
Functions 3.5 |
A function shall be no longer than 100 NCSL. |
A return type of a function shall always be explicitly specified. |
A function shall never return a reference or a pointer to a local variable. |
Functions that return pointers shall return a NULL on a failure condition. |
If a function can fail, the function shall return a status indicating a failure. |
Declaring the magnitude of a single dimensional array in an argument declaration shall not be used. |
A main function shall return a value indication the completion status. |
Structures shall not be passed as arguments to functions and functions shall not return structures. |
The const modifier shall be used in the parameter list for all parameters which are not changed by the function. |
General Coding Practices 3.6 |
All source code shall use the POPI classification Motorola Confidential Proprietary. |
Inline assembly shall not be used. |
Include Files 3.7 |
Every include file shall contain a mechanism that prevents multiple inclusions of the file. |
All #include directives shall be placed at the top of the file, just below the introductory comments and revision history. |
Macros 3.8 |
Arguments to macros shall not produce side-effects to the arguments themselves. |
Macro parameters shall be fully parenthesized. The entire macro shall also be enclosed in parenthesis. |
Macros shall not be used to change the nature of the language. |
A macro that produces executable code shall be written as a parameterized macro, even if no parameters are needed. |
Naming Convention 3.9 |
All identifier name lengths shall be less than or equal to 31 characters. |
Legal identifier name values shall be those specified by the ANSI C standard. |
Underscore shall not be used as the first character of an identifier. |
Consistent variable naming conventions shall be used. |
Variables 3.10 |
All variable declarations shall include data types. |
Every variable that is declared shall be given a value before it is used. |
The size of the least significant (rightmost) dimension of an externally declared array variable shall not be specified. |
An externally declared array variable shall never be declared as a pointer. |
Use “void *” (or VOID *) instead of “char *” to represent a generic pointer. |
Type conversions shall be explicitly cast. |
Classes 4.1 |
The public, protected, and private sections of a class shall be declared once in the order listed. |
Member functions shall not be defined within the class declaration. |
Member data in a class shall be declared private, except for static constant data. |
A member function that does not affect the state of an object is to be declared const. |
All classes shall declare a copy constructor. |
A copy constructor of a derived class shall call the copy constructor of its base class. |
A class which uses “new” to allocate instances managed by a class shall define a destructor which deallocates the memory. |
All classes which are used as base classes shall define a virtual destructor. |
The constructors of an abstract class shall be declared as protected. |
A class’ virtual functions shall not be called from within its constructors. |
An initializer shall not call member functions |
All classes shall declare an assignment operator. |
All assignment operators shall guard against performing the action on itself. |
An assignment operator of a derived class shall call the assignment operator of its base class. |
Comments 4.2 |
Every file that contains source code shall be documented with an introductory comment that provides information on the file name, its contents, and copyright information. |
Every file that contains source code shall contain a revision history. |
Files containing more that one function shall contain a function header for each function. |
Code shall not be commented out. |
Comments shall not be nested. |
Constants 4.3 |
const shall be used instead of #define. |
NULL shall not be used. |
Constants shall be used instead of hard coded numbers. |
Exceptions 4.4 |
Classes shall be used to represent exceptions. |
Destructors shall prevent exceptions from escaping. |
Exceptions shall be caught by reference. |
Expressions 4.5 |
Assumption about any particular expression evaluation order shall be avoided. |
Implicit logic tests shall be avoided. |
All conditional expressions shall be explicit. |
Float values shall not be tested for equality. |
Flow Control Statements 4.6 |
The code following a case label shall be terminated by a break statement. |
A ‘switch’ statement shall contain a ‘default’ branch that handles unexpected cases in an appropriate way. |
A “goto” statement shall never be used. |
A null statement as body of a loop shall include a comment to make the intent clear. |
Functions 4.7 |
A function shall be no longer than 100 NCSL. |
A return type of a function shall always be explicitly specified. |
A function shall never return a reference or a pointer to a local, private or protected variable. |
If a function can fail, the function shall return a status indicating a failure or throw an exception. |
Functions (cont.) |
Functions that return pointers shall return 0 or throw an exception on a failure condition. |
A main function shall return a value indication the completion status. |
The magnitude of a single dimensional array in an argument declaration shall not be used. |
The preprocessor directive #define shall not be used to obtain more efficient code; instead, inline functions shall be used. |
One line accessor and modifier functions (e.g. getValue, setValue) shall be inline. |
General Coding Practices 4.8 |
All source code shall use the POPI classification Motorola Confidential Proprietary. |
Inline assembly shall not be used. |
Include Files 4.9 |
Only one class shall be declared per header file. |
Every include file shall contain a mechanism that prevents multiple inclusions of the file. |
Header files shall be self sufficient. |
Memory Allocation 4.10 |
Malloc, realloc and free shall not be used. |
Empty brackets ("[]") shall be provided for delete when deallocating arrays. |
Naming Convention 4.11 |
Legal identifier name values shall be those specified by the ANSI C++ standard. |
Consistent naming conventions shall be used. |
Type Conversion 4.12 |
C++ style casts shall be used instead of C style. |
Variables 4.13 |
Every variable that is declared shall be given a value before it is used. |
The size of the least significant (rightmost) dimension of an externally declared array variable shall not be specified. |
Type conversions shall be explicitly cast. |