Table of contents
In embedded and high-performance applications, it's hard to write code.
-
Keep functions small and focused
-
Use descriptive names
-
DRY don't repeat yourself
-
Use abstractions (try to abstract and mock your hardware to test on regular machines.) Abstractions should have multiple layers so you can think about them in simpler terms.
-
Use a TTD like gtest, as rigorously as possible.
-
Optimize late: get things working and then benchmark to tune
-
Write for humans, not computers (favor readable code)
-
if something confusing/difficult, write some utilities around it that make it harder to use incorrectly (define your API to guide your users)
-
whenever a computer/tool can do something better than humans, use it and make it impossible to not use.
-
Sanitizers (AddressSanitizer, UndefinedBehaviorSanitizer)
-
Linters (clang-tidy)
-
Code formatting (clang-format)
-
Compile with
-WError
as many warnings -
Compile at least hardware-abstracted unit tests) with multiple modern compilers.
References
Roedy Green, unmaintanable code, Accessed 2024-04-03, [Online]
MISRA, MISRA C:2012 Permits, Published March 2021, [Online]