04 CodeStyleHumans
04 CodeStyleHumans
for Humans
Prof. Philip Koopman
Comments
Explain what & why, not just code paraphrase
Comments are not a design
Naming
Descriptive, consistent naming conventions
– E.g., variables are nouns; functions are verbs
Avoid magic numbers (use const)
Avoid macros (use inline) http://blog.aerojockey.com/post/iocccsim
© 2020 Philip Koopman 3
Good Code Hygiene
Modularity
Many smaller .c/.cpp files (one per class)
Externally visible declarations into .h file
Conditional Statements
Boolean conditional expression results; no assignments
All switch statements have a default (usually error trap)
Limited nesting (see also cyclomatic complexity)
Variables
Descriptive names that differ significantly
Smallest practicable scope for variables; initialize at point of definition
Use typedefs to define narrow types (also use uint32_t, use enum, etc.)
Range checks & bounds checks (e.g., buffer overflow)
(Author unclear)