- Class Envy
- Inherited Fear
- The Forgotten Benefit: Debugging
- Hope for C Programmers
- Conclusion
The Forgotten Benefit: Debugging
In addition to the class construct, you should utilize the const keyword instead of using #define. Unlike #define values, which are invisible to debuggers, const variables are placed in the symbol table and can be seen by debuggers such as gdb. Consequently, it's much easier to debug code that relies on const.
More importantly, const can be used to create reliable public interfaces. For example, the const keyword enables you to dictate which parameters and return values in the public interfaces can be modified. This eliminates the possibility of variables being mistakenly trampled by engineers who are unfamiliar with the design goals of the interface. const also offers the added bonus of type checking during assignment and comparison operations. As a result, proper usage of const increases overall system reliability.
You should also leverage the inline keyword in your embedded applications. The most obvious benefit of inline is that it boosts performance; you don't have the overhead of a function call. Yet most people are unaware that inline functions can also dramatically ease embedded debugging. Specifically, older embedded projects tend to be littered with many layers of macros. If you encounter a bug in one of these macros, debugging can be an excruciating process. By contrast, if the macro is replaced by a const inline function, the inline function is easily debugged.
Alas, the inline keyword has one nasty side effect: code bloat. In other words, every time you use inline, a non-optimizing compiler inserts a copy of the function into the code. Therefore, if you use an inline function 100 times, your code may be 100 times larger than the equivalent non-inline code (the actual size will vary by compiler and processor).