Return to site

CODE SMELL: Knowledge duplication 📝📝

· coding,code-smell

Knowledge duplication happens when the same codified knowledge is found in multiple places. The focus is on knowledge, rather than only on code. It will be inevitable to have similar code parts, and it's not always the best strategy to unify them. If they represent different pieces of knowledge, it's not wrong to keep them separate. If, however, they represent the same unit of knowledge (for example, code to read a file or to parse HTTP responses is copied in multiple places), then duplication is harmful.

Why is it bad? 👎

When a certain piece of duplicated knowledge changes, we need to look throughout the code to update every duplication.

Different instances of the duplicated knowledge in the codebase may be doing different things while being expected to do the same thing (over time, these duplications tend to diverge in functionality and be 'overfit' to the places where they are being used).

How to fix it? 💡

Extract the duplicated knowledge into helper functions or classes.

Use the helper functions or classes throughout the code, but make sure not to overgrow them. They should still follow the SOLID principles.

Avoid extracting code that represents different knowledge. This increases the coupling between unrelated parts of the domain.