Member-only story

Removing Code Duplication

Rafael A. George Duval
2 min readNov 7, 2022

--

The first rule development teams often follow is don’t repeat yourself — DRY. Removing duplication is an easy rule to follow, even for novice programmers.

The downside of the rule is that, for the most part, it is executed based on visible similarities of the code rather than conceptual ones.

The problem with following the rule might lead the team to the wrong abstraction. It takes a lot of effort to deal with the wrong abstraction. It is far cheaper to keep duplication than to have a false conception.

Making a system DRY is difficult because we cannot assume that every piece of identical code is duplicated.

Second time is not duplication but implementation. Third time is when you remove it. Hard to see duplication on a pull request.

Developers with more experience tend to tolerate more duplication and wait for a better abstraction.

Finding the right abstraction implies understanding concepts at a higher level, which are only sometimes clear.

To deal with this problem, I make myself accountable for the duplication by quantifying it. I use a DUP tag at the top of a piece of code that I’ve already seen.

Dup Tag

1. Use an ID to group duplication

2. Before abstracting out code, quantify the number of dup IDs.

3. Wait for at least three occurrences of the same code to abstract it.

--

--

Rafael A. George Duval
Rafael A. George Duval

Written by Rafael A. George Duval

✍🏼 Building a Solo Digital Media Company 🧪 Snippets of Text [https://snippetsoftext.substack.com/subscribe]

No responses yet