The Netrino Embedded C Coding Standard is available in book form (view Table of Contents) and details a set of guiding principles as well as providing specific rules for modules, data types, functions, macros, and variables. The coding standard incorporates enhancements that have been proven to increase firmware quality by keeping bugs out—while also improving maintainability and portability. Rules that have been demonstrated to reduce or eliminate certain types of bugs are highlighted with the Zero Bugs...Period logo.
The Netrino Embedded C Coding Standard is provided free to attendees of all Netrino Institute courses. Check out the current course calendar.
Guiding Principles
This coding standard was developed according to the following guiding principles, which serve to reduce conflict over certain items that could otherwise be viewed as stylistic preferences:
- There are many sources of bugs in software programs. Some bugs are created by the original programmer. Other bugs result from misunderstandings of the original code by those who maintain, extend, and/or reuse (or port) it at a later time.
- The number of bugs introduced by the original programmer can be reduced through the disciplined application of certain coding practices, such as the placement of constants on the left side of an equivalence (==) test.
- Some types of bugs caused by maintenance users can be prevented by the original programmer. For example, appropriate use of compiler-aware fixed-width integer types (e.g., int16_t) ensures that future ports of the code will not encounter unexpected overflow.
- The number of bugs introduced by programmers who later touch the code can be reduced through the disciplined use of consistent commenting and stylistic practices, so that anyone in the organization can more easily understand the purpose and proper use of variables, functions, and modules.
- It is cheaper and easier to prevent a bug from creeping into code than it is to find and kill it later. A key strategy in this fight is to write code in which bugs can be detected by the compiler, linker, or a static analysis tool—that is, before the code is allowed to execute.
- For better or worse (mostly worse ;-), the C programming language “standard ” allows for a significant amount of variability in the decisions made by compiler implementers. These so-called “implementation-defined,” “unspecified,” and “undefined” behaviors, plus “locale-specific options” mean that programs compiled from identical C source code may behave very differently at run-time. These gray areas in the language standard greatly reduce the portability of C programs that are not carefully crafted; this coding standard prioritizes portability above efficiency or programmer convenience.
- MISRA’s Guidelines for the Use of the C Language may seem onerous but are worthy of study. Deviation from the MISRA guidelines should not be taken lightly.
- To be effective, coding standards must be enforceable. Whenever possible, rules that allow for automated enforcement are preferable to those requiring manual enforcement.
- In the absence of a needed rule or a conflict between rules, the spirit of these principles should be applied to guide the decision.
- Individual programmers do not own the software they write. All coding is work for hire for an employer or a client and, thus, should be constructed in a workmanlike manner.
Table of Contents
- Introduction
- Purpose of the Standard
- Guiding Principles
- Enforcement Procedure
- Deviation Procedure
- General Rules
- Which C?
- Line Width
- Linefeed Characters
- Comments
- Doxygen
- Braces
- Parentheses
- Indentation
- Tabs
- White Space
- Common Abbreviations
- Keywords to Avoid
- Keywords to Frequent
- Modules
- Naming Convention
- Header Files
- Source Files
- File Templates
- Data Types
- Naming Conventions
- Fixed-Width Integers
- Floating Point Types
- Procedures
- Naming Convention
- Functions
- Macros
- Tasks
- Interrupt Service Routines
- Variables
- Naming Convention
- Initialization
- Expressions
- Switch Statements
- If-Else Statements
- Comparisons
- References
- Appendix A: Template Header File
- Appendix B: Template Source File
The Netrino Embedded C Coding Standard is provided free to attendees of all Netrino Institute courses. Check out the current course calendar.