Hacker News new | ask | show | jobs
by BurningFrog 1620 days ago
My slightly different heuristic:

If fact X changes, how many places in the code do we have to change it?

If the number is > 1, relying on humans to realize that will, on average, always fail. I know of two ways to fix that.

[1]: DRY it up.

[2]: Write a test asserting that all the places use the same value.

Sadly, most people rely on this:

[3]: We "just have to remember" to do these changes in all places, despite the fact that human memory is unreliable, and the future person working on this code isn't even in the room when we decided this.

Any time I hear a "we just have to remember" variation, either from coworkers or in my thoughts, an alarm goes off.

1 comments

[2b] is to use the type system to make sure the places are consistent, the practicality of which depends significantly on both particular task and choice of technology.

[3b] is adding comments, which is better than relying purely on memory but can often be missed. I've locally improved on this a bit by including cross-references on the relevant lines, along with tooling such that the referents are automatically surfaced during code review.