Netrino's Embedded C Coding Standard was developed from the ground up to minimize bugs in firmware, by focusing on practical rules that keep bugs out—while also improving the maintainability and portability of embedded software. The coding standard details a set of guiding principles (more below) as well as specific naming conventions and other rules for the use of data types, functions, preprocessor macros, variables and much more. Individual rules that have been demonstrated to reduce or eliminate certain types of bugs are highlighted.
A printed copy of Netrino's Embedded C Coding Standard is provided to each attendee of Netrino's Developing Effective Coding Standards course (see our public calendar for dates and locations). The book is also available for purchase by mail for $29 (includes shipping within the U.S.); use the "Buy Now" button below to order your copy.
Table of Contents
|
| ISBN 1-4421-6482-4 |
|
|
Clicking "Buy Now" will take you to Paypal (www.paypal.com) to place your order.
|
- Introduction
- Purpose of the Standard
- Guiding Principles
- Enforcement Procedure
- Deviation Procedure
- General Rules
- Which C?
- Line Width
- Braces
- Parentheses
- Common Abbreviations
- Casts
- Keywords to Avoid
- Keywords to Frequent
- Comments
- Acceptable Formats
- Location and Content
- White Space
- Spaces
- Alignment
- Blank Lines
- Indentation
- Tabs
- Linefeeds
- Modules
- Naming Conventions
- Header Files
- Source Files
- File Templates
- Data Types
- Naming Conventions
- Fixed-Width Integers
- Signed Integers
- Floating Point
- Structures and Unions
- Procedures
- Naming Conventions
- Functions
- Function-Like Macros
- Tasks
- Interrupt Service Routines
- Variables
- Naming Conventions
- Initialization
- Expressions and Statements
- Variable Declarations
- If-Else Statements
- Switch Statements
- Loops
- Unconditional Jumps
- Equivalence Tests
- Bibliography
- Appendix A: Header File Template
- Appendix B: Source File Template
- Appendix C: Common Abbreviations
Guiding Principles (excerpt)
This coding standard was developed in accordance with the following guiding principles, which served to focus the authors’ attention and eliminate conflict over items that are sometimes viewed by programmers as personal stylistic preferences:
- Individual programmers do not own the software they write. All software development is work for hire for an employer or a client and, thus, the end product should be constructed in a workmanlike manner.
- It is cheaper and easier to prevent a bug from creeping into code than it is to find and kill it after it has entered. A key strategy in this fight is to write code in which the compiler, linker, or a static analysis tool can detect bugs automatically—i.e., before the code is allowed to execute.
- For better or worse (well, mostly worse), the ISO “standard ” C programming language allows for a significant amount of variability in the decisions made by compiler implementers. These many so-called “implementation-defined,” “unspecified,” and “undefined” behaviors, along with “locale-specific options”, mean that programs compiled from identical C source code may behave very differently at run-time. Such gray areas in the language standard greatly reduce the portability of C programs that are not carefully crafted.
- This coding standard prioritizes code reliability and portability above execution efficiency or programmer convenience.
- There are many sources of bugs in software programs. The original programmer creates some bugs. Other bugs result from misunderstandings by those who later maintain, extend, port, and/or reuse the code.
- The number and severity of bugs introduced by the original programmer can be reduced through disciplined conformance with certain coding practices, such as the placement of constants on the left side of an equivalence (==) test.
- The number and severity of bugs introduced by maintenance programmers can also be influenced by the original programmer. For example, appropriate use of portable fixed-width integer types (e.g., int32_t) ensures that no future port of the code will encounter an unexpected overflow.
- The number and severity of bugs introduced by maintenance programmers can also be reduced through the disciplined use of consistent commenting and stylistic practices, so that everyone in an organization can more easily understand the meaning and proper use of variables, functions, and modules.
- MISRA’s Guidelines for the Use of the C Language are more restrictive than this coding standard—but worthy of study. Deviation from any MISRA-C required or advisory rule should be carefully considered. The authors of the MISRA-C guidelines are knowledgeable of the risks of the use of C in safety-critical systems. Our few known differences of opinion with [MISRA04] are identified in the footnotes to this standard. Followers of Netrino’s coding standard may wish to adopt the other rules of MISRA-C in addition to the rules found here.
- To be effective, coding standards must be enforceable. Wherever two or more competing rules would be similarly able to prevent bugs but only one of those rules can be enforced automatically, the more enforceable rule is recommended.
In the absence of a needed rule or a conflict between rules, the spirit of the above principles should be applied to guide the decision.
Bug-Killing Coding Rules (article)
You can find examples of the kinds of bug-killing embedded C coding standard rules we follow in
Michael Barr's
April 2009 and
May 2009 columns in
Embedded Systems Design magazine as well as from time to time in his blog at
http://www.embeddedgurus.net/barr-code.
How to Buy
A printed copy of Netrino's Embedded C Coding Standard is provided to each attendee of Netrino's Developing Effective Coding Standards course (see public calendar for dates and locations).
The Embedded C Coding Standard book is available for purchase directly from Netrino for just $29 (price includes free shipping within the U.S.; international shipping adds $10). Use the "Buy Now" button above to order your copy.
Alternatively, you may purchase the book from retailers such as Amazon.com, where the price has recently been around $40 (plus shipping).
Or consider buying the book bundled with the online webinar How to Keep Bugs out of Embedded Software with Coding Standards for $99 (price includes free shipping worldwide).
Call 866-78-EMBED to discuss electronic copies or to license the editable source document (e.g., .doc).
Fantastic Book!
Best standard I've seen for developing firmware in C
A quality coding standard for Embedded C Programmers
If you are a program manager, project manager or team lead of an embedded systems project, get this book, give a copy to everyone on your team and use it ragged until your team produces consistent code. You can not go wrong using the standard supplied by this book and there are many, many ways to go wrong using some other standard or none at all.
-- Rob Wehrli (excerpt) at Amazon.com