Representative Line: Three's a Constant
Bryant E was debugging a hardware system with some embedded C code. Due to form factor considerations, there weren't a lot of physical inputs to this device, which meant what controls it had needed to be reused cleverly.
For example, there was a pushbutton. Just pushing it performed one function. Press-and-hold did another. Double click and triple click did yet more functions.
Or at least, they were supposed to. Skimming through the code, it looked correct. Bryant saw things like if (clickCount == THREE_CLICKS). Could it be any more clear?
While trying to understand why the triple click wasn't working, that constant nagged at him. It's one thing to remove magic numbers, it's something completely different to define a constant explicitly named THREE_anything. When Bryant repeatedly tapped the button as quickly as he could, and saw the triple click event fire after far more than three clicks, he started to get a sense of what might be going on.
This, as it turned out, was the constant definition:
// a click is a press and release event#define THREE_CLICKS 6
Perhaps, at one point, this constant was meant to track something different than it was actually used for. If there are six press and release events, that is three clicks, so maybe the original developer meant it to be a count of events. That would almost make sense except that:
#define TWO_CLICKS 2
The moral of the story is never trust a constant that contains a number in its name. It's often lying to you.
[Advertisement] Continuously monitor your servers for configuration changes, and report when there's configuration drift. Get started with Otter today!