![]() |
mTower
|
File header. Every *.c, *.h, Makefile, or *.sh begins with a file header. Thatfile header is enclosed with a block comment:
Find example into appendix A.
The following groupings should appear in all C source files in the following order:
The following groupings should appear in all C header files in the following order:
Header File Idempotence. C header file must protect against multiple inclusion through the use of macros that "guard" against multiple definitions if the header file is included multiple times.
Each header file must contain the following pre-processor conditional logic near the beginning of the header file: Between the file header and the "Included Files" block comment. For example,
Forming Guard Names. Then pre-processor macro name used in the guard is formed from the full, relative path to the header for from the top-level, controlled directory. That pat is preceded by __ and _ replaces each character that would otherwise be invalid in a macro name. So, for example, __INCLUDE_CRYPTO_AES_H corresponds to the header file include/crypto/aes.h
Deoxygen Information. mTower uses Deoxygen for documentation.
Sample File Formats. C source and header file templates are provided in an Appendix to this document.
Line Endings. Files should be formatted with
(Unix line endings). There should be no extra whitespace at the end of the line. In addition, all text files should end in a single newline (
). This avoids the "No newline at end
of file" warning generated by certain tools.
Line Width. Text should not extend past column 80 in the typical C source or header file.
Indentation Comments should, typically, be placed before the code section to which they apply.
Short, Single line comments. Short comments must lie on a single line. The comment delimiters must lie on the same line.
Incorrect
Correct
Multi-line comments. If the comment is too long to fit on a single, it must be broken into a multi-line comment. The comment must be begin on the first line of the multi-line comment with the opening comment delimiter /*. The following lines of the multi-line comment must be with an asterisk * aligned in the same column as the asterisk in the preceding line. The closing comment delimiter must lie on a separate line with the asterisk * aligned in the same column as the asterisk in the preceding line.
Incorrect
Correct
Comments to the Right of Data Definitions. Comments to the right of a declaration with an enumeration or structure, on the other hand, are encouraged, provided that the comments are short and do not exceed the maximum line width (usually 80 characters). Columnar alignment of comments is very desireable (but often cannot be achieved without violating the line width).
Incorrect
Correct
C Style Comments. C99/C11/C++ style comments (beginning wih //) should not be used with mTower. mTower generally follows C89 and all code outside of architecture specific directories must be compatible with C89.
Incorrect
Correct
**"Commenting Out" Large Code Blocks.** Do not use C or C++ comments to disable compilation of large blocks of code. Instead, use #if 0 to do that. Make sure there is a comment before the #if 0 to explain why the code is not compiled
Incorrect
Correct
Never Comments on Braces. Do not put comments on the same line as braces.
The preferred way, as shown to us by the prophets Kernighan and Ritchie, is to put the opening brace last on the line, and put the closing brace first, thusly:
Incorrect
Correct
This applies to all non-function statement blocks (if, switch, for, while, do):
Correct
However, there is one special case, namely functions: they have the opening brace at the beginning of the next line, thus:
Correct
Indentation Unit. Indentation is in units of two spaces; Each indentation level is twos spaces further to the right than the preceding identation levels.
Indentation of Pre-Processor Lines. C Pre-processor commands following any conditional computation are also indented following basically the indentation same rules, differing in that the # always remains in column 1.
Incorrect
Correct
Exception. Each header file includes idempotence definitions at the beginning of the header file. This conditional compilation does not cause any change to the indentation.
Correct
Space after key words. Do not put a left parenthesis ( immediately after any C keywords (for, switch, while, do, return, etc.). Put a space before the left parenthesis in these cases.
Otherwise, no space before left parentheses. Otherwise, there should be no space before the left parentheses No space betwen function name and argument list. There should be no space between a function name and an argument list.
Never space before the right parentheses. There should never be space before a right parenthesis ).
No parentheses around returned values. Returned values should never be enclosed in parentheses unless the parentheses are required to force the correct order of operations in a computed return value.
Incorrect
Correct
Incorrect
Correct
Short global variable names. Names should be terse, but generally descriptive of what the variable is for. Try to say something with the variable name, but don't try to say too much. For example, the variable name of filelen is preferable to something like lengthoffile.
Module name prefix. If a global variable belongs in a module with a name of, say xyz, then that module should be included as part of the prefix like: xyz_.
lowerCamelCase. Use lowerCamelCase style.
Minimal use of '_'. Preferably there are no '_' separators within the name. Long variable names might require some delimitation using '_'. Long variable names, however, are discouraged.
Use structures. If possible, wrap all global variables within a structure to minimize the liklihood of name collisions.
Avoid global variables when possible. Use of global variables, in general, is discourage unless there is no other reasonable solution.
Incorrect
Correct
Common naming standard. Naming for function parameters and local variables is the same.
Short variable names. Names should be terse, but generally descriptive of what the variable is for. Try to say something with the variable name, but don't try to say too much. For example, the variable name of len is preferable to something like lengthofiobuffer.
No special ornamentation. There is no special ornamentation of the name to indication that the variable is a local variable. The prefix p or pp may be used on names of pointers (or pointer to pointers) if it helps to distinguish the variable from some other local variable with a similar name. Even this convention is discouraged when not necessary.
lowerCamelCase. Use lowerCamelCase style.
Minimal use of single character variable names. Short variable names are preferred. However, single character variable names should be avoided. Exceptions to this include i, j, and k which are reserved only for use as loop indices (part of our Fortran legacy).
Minimal use of '_'. Preferably there are no '_' separators within the name. Long variable names might require some delimitation using '_'. Long variable names, however, are discouraged.
Incorrect
Correct
NOTE: You will see the local variable named ret is frequently used in the code base for the name of a local variable whose value will be returned or to received the returned value from a called function.
Short type names. Type names should be terse, but generally descriptive of what the type is for. Try to say something with the type name, but don't try to say too much. For example, the type name of fhandle_t is preferable to something like openfilehandle_t.
Type name suffix. All typedef'ed names end with the suffix _t.
Module name prefix If a type belongs in a module with a name of, say xyz, then that module should be included as a prefix to the type name like: xyz_.
lowerCamelCase. Use lowerCamelCase style.
Minimal use of '_'. Preferably there are few _ separators within the type name. Long type names might require some delimitation using _. Long type names, however, are discouraged.
Incorrect
Correct
No un-named structures. All structures must be named, even if they are part of a type definition. That is, a structure name must follow the reserved word struct in all structure definitions.
Structured defined with structures discouraged. Fields within a structure may be another structure that is defined only with the scope of the containing structure. This practice is acceptable, but discouraged.
No un-named structure fields. Structure may contain other structures as fields. This this case, the structure field must be named.
Short structure names. Structure names should be terse, but generally descriptive of what the structure contains. Try to say something with the structure name, but don't try to say too much. For example, the structure name of xyz_info_s is preferable to something like xyz_datainputstatusinformation_s.
Structure name suffix. All structure names end with the suffix _s.
Module name prefix If a structure belongs to a module with a name of, say xyz, then that module should be included as a prefix to the structure name like: xyz_.
lowerCamelCase. Use lowerCamelCase style.
Minimal use of '_'. Preferably there are few _ separators within the structure name. Long variable names might require some delimitation using _.
Common variable naming. Structure field naming is generally the same as for local variables.
One definition per line. The one definition per line rule applies to structure fields, including bit field definitions.
Each field should be commented. Each structure field should be commented.
Optional structure field prefix. It make be helpful to add a two-letter prefix to each field name so that is is clear which structure the field belongs to.
lowerCamelCase. Use lowerCamelCase style.
Minimal use of '_'. Preferably there are few _ separators within the field name. Long variable names might require some delimitation using _. Long variable names, however, are discouraged.
Incorrect
Correct
Enumeration Naming. Naming of enumerations follow the same naming rules as for structure and union naming. The only difference is that the suffix _e is used to identify an enumeration.
Uppercase. Enumeration values are always in upper case.
Use of '_' encouraged. Unlike other naming, use of the underscore character _ to break up enumeration names is encouraged.
Prefix. Each value in the enumeration should begin with an upper-case prefix that identifies the value as a member of the enumeration. This prefix should, ideally, derive from the name of the enumeration.
Correct
Uppercase. Macro names are always in upper case.
Macros that could be functions. Lower-case macro names are also acceptable if the macro is a substitute for a function name that might be used in some other context. In that case, normal function naming applies.
Use of '_' encouraged. Unlike other naming, use of the underscore character _ to break up macro names is encouraged.
Prefix. Each related macro value should begin with an upper-case prefix that identifies the value as part of a set of values.
Single space after #define. A single space character should separate the #define from the macro name. Tabs are never used.
Line continuations. Macro definitions may be continued on the next line by terminating the line with the \ character just before the newline character. There should be a single space before the \ character. Aligned \ characters on multiple line continuations are discouraged because they are a maintenance problem.
Parentheses around macro argument expansions. Macros may have argument lists. In the macros expansion, each argument should be enclosed in parentheses.
Real statements. If a macro functions as a statement, then the macro expansion should be wrapped in do { ... } while (0) to assume that the macros is, indeed, a statement.
Magic numbers are prohibited in code. Any numeric value is not intuitively obvious, must be properly named and provided as either a pre-processor macro or an enumeration value.
Incorrect
Correct
Function headers. Each function is preceded by a function header. The function header is a block comment that provides information about the function. The block comment consists of the following:
Function header preceded by one blank line. Exactly one blank line precedes each function header.
Function header followed by one blank line. Exactly one blank line is placed after function header and before the function definition
Function header sections. Within the function header, the following data sections must be provided:
Each of these data sections is separated by a single line like " * ".
Function header template. Refer to Appendix A for the template for a function header.
Short function names. Function names should be terse, but generally descriptive of what the function is for. Try to say something with the function name, but don't try to say too much. For example, the variable name of xyz_putvalue is preferable to something like xyz_savethenewvalueinthebuffer.
lowerCamelCase. Use lowerCamelCase style.
Module prefix. All functions in the same module, or sub-system, or within the same file should have a name beginning with a common prefix separated from the remainder of the function name with the underscore, _, character. For example, for a module called xyz, all of the functions should begin with xyz_.
Extended prefix. Other larger functional grouping should have another level in the naming convention. For example, if module xyz contains a set of functions that manage a set of I/O buffers (IOB), then those functions all should get naming beginning with a common prefix like xyz_iob_.
Use of '_' discouraged. Further use of the _ separators is discouraged in function naming. Long function names might require some additional elimitation using _. Long function names, however, are also discouraged.
Single compound statement. The function body consists of a single compound statement.
Braces in column 1 The opening and close braces of the compound statement must be placed in column one.
First definition or statement in column 3. The first data definitions or statements in the function body are idented by two space.
Long functions are discouraged. The length of a function should be limited so that it would fit on a single page.
Space after the function boady. A one (and only one) blank line must follow the closing right brace of the function body.
OS Internal Functions. In general, OS internal functions return a type int to indicate success or failure conditions. Non-negative values indicate success. The return value of zero is the typical success return value, but other successful return can be represented with other positive values. Errors are always reported with negative values. These negative values must be a well-defined jerrno as defined in the file include/jerrno.h.
Checking Return Values. Callers of internal OS functions should always check return values for an error. At a minimum, a debug statement should indicate that an error has occurred. The calling logic intentionally ignores the returned value, then the function return value should be explicitly cast to (void) to indicate that the return value is intentionally ignored. All calls to malloc or realloc must be checked for failures to allocate memory.
One statement per line. There should never be more than one statement on a line.
No more than one assignment per statement. Related to this, there should never be multiple assignments in the same statement.
Statements should never be on the same line as any keyword. Statements should never be on the same line as case selectors or any C keyword.
Incorrect
Correct
88Spaces before and after binary operators.** All binary operators (operators that come between two values), such as +, -, =, !=, ==, >, etc. should have a space before and after the operator, for readability. As examples:
Incorrect
Correct
Incorrect
Correct
<condition> ? <then> : <else>
Only if the expression is short. Use of this form is only appropriate if the entire sequence is short and fits neatly on the line.
Multiple lines forbidden. This form is forbidden if it extends to more than one line.
Correct
Correct
Incorrect
Correct
Incorrect
Correct
Limited Usage of goto. All use of the goto statement is prohibited except for one usage: for handling error conditions in complex, nested logic. A simple goto in those conditions can greatly improve the readability and complexity of the code.
Label Naming. Labels must all lower case.
Label Positioning. Labels are never indented. Labels must always begin in column 1.
Correct
```c /**
1.8.13