A code review encounter with repeated numeric literals in C firmware illustrates the classic 'magic numbers' problem. Magic numbers are unnamed constants used repeatedly in source code, violating the DRY principle. They are especially problematic in embedded software, where constants often come from hardware specs, memory maps, and calibration values. The fix is straightforward: replace raw literals with named constants (#define, constexpr) accompanied by meaningful comments referencing the relevant specification. This creates a single point of change for maintenance and improves code readability.

5m read timeFrom embeddedrelated.com
Post cover image
4 Impressions