This document discusses code style best practices for compilers, including using language features like enums and typedefs to improve safety and avoid bugs, following guidelines like MISRA C and CERT, and using static and dynamic analysis tools. It also discusses strategies for handling legacy code and notes safer languages like Spark Ada which use formal methods.
This document discusses code style best practices for compilers, including using language features like enums and typedefs to improve safety and avoid bugs, following guidelines like MISRA C and CERT, and using static and dynamic analysis tools. It also discusses strategies for handling legacy code and notes safer languages like Spark Ada which use formal methods.
Let the Language Help! Use enum instead of int enum color {black, white, red}; // avoids bad values Use const instead of #define const uint64_t x = 1; // helps with type checking uint64_t y = x << 40; // avoids 32-bit overflow bug Use inline instead of #define If it’s too big to inline, the call overhead doesn’t matter Many compilers inline automatically even without keyword
Use typedef with static analysis
typedef uint32_t feet; typedef uint32_t meters; feet x = 15; meters y = x; // feet to meters assignment error https://goo.gl/6SqG2i