|
|
|
|
|
by ebiester
1620 days ago
|
|
My first heuristic is: If I change the code in block A, is it assured that I will need to change the code in block B? My second heuristic is: Can I name the method I wish to de-duplicate in a way that is honest for all cases I wish to cover, yet explains its business purpose? The more it deviates from these heuristics, the more likely I am to duplicate the code in object oriented programming. |
|
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.