EMBEDDED SYSTEMS & MICROCONTROLLERS · PAGE 38
Defensive Firmware Practices
Code that survives the real world.
Check every input range, initialise every variable, and give each loop a way out. Assume sensors fail and wires come loose.
Use constants rather than magic numbers, keep functions short, and avoid recursion on small stacks. Add a watchdog and log reset causes.
Test the edges: minimum, maximum, zero and nothing connected.