Today I read Dan Abramov’s article about clean code and as I feel pretty strongly about clean/readable/understandable code I wanted to put my thoughts about that topic on (digital) paper.
The points in the article are objectively true and probably helpful for lots of people but at the same time, I think it misses an important point.
The DRY principle is stated as “Every piece of knowledge must have a single, unambiguous, authoritative representation within a system”. (wikipedia)
For me, DRY is not about saving words or characters and making your code shorter. Gzip will do that better than you anyway.
I think the important part is to understand what “Every piece of knowledge” means. For me, it means that first and foremost making your code DRY should convey information, which is that those two things are actually one thing even though they are used or referenced from different places. So DRY is a very important tool to convey crucial information to others.
It is important to understand that this concept of what is a “piece of knowledge” does not necessarily map one to one to text. Sometimes the same code can represent distinct concepts or two parts of code that are different textually represent the same concept.
Get Andrej Ocenas’s stories in your inbox
Join Medium for free to get updates from this writer.
This principle also isn’t on a sliding scale between duplication and no abstraction up to being DRY with too much abstraction. Choosing the appropriate abstraction for what the code does is important but you can make your code DRY with very little abstraction or have lots of duplication while using convoluted and complex abstraction.
Secondly, nothing is free. My code traded the ability to change requirements for reduced duplication, and it was not a good trade. For example, we later needed many special cases and behaviors for different handles on different shapes. My abstraction would have to become several times more convoluted to afford that, whereas with the original “messy” version such changes stayed easy as cake. (Dan’s article)
To some, this may suggest that there is a correlation between duplication and the changeability of the code. I think this creates a straw-man argument saying that the only way forward after the refactor is to keep the abstraction even though the underlying concepts change in a way that the abstraction is no longer appropriate.
I think this is what lots of people get wrong. In my opinion, code that is easiest to change is not one that was written with the change in mind and needs the least amount of textual diff to incorporate the new requirements. The code that is easiest to change is one that is most readable and most accurately represents the system it models at the current time, which makes it easy to understand what needs to change and how.
The easiest system to change is the one that is easiest to understand.
So without saying whether the refactor was a good thing or not, the important consideration isn’t about whether it better predicted the future requirements or how much new code it would need to change it. For me, the more important question would be whether it sensibly modelled the current state of the system, whether the code after the refactor was easier to understand than before and by extension easier to change with confidence.
The answer still may be “No”. Maybe the abstraction was a bit too complex for this case and it was harder to understand than before. Maybe the code that was almost the same did not represent the same “Piece of knowledge” or it did and would actually benefit from making it DRY. Even if it had to be unDRYed later when it no longer represented the system correctly, the change may have been easier even while changing the abstraction.
So to summarise: Don’t just sling patterns, principles or abstractions willy nilly. Understanding is usually the most important thing. You rise understanding by proper communication, in this context with your code. So when changing code, ask yourself whether your change makes it easier for others to understand the system or not.




![“Lines Crossed in Sin City!” [S1EP5]](https://miro.medium.com/v2/resize:fit:1358/format:webp/1*R88bHuVrOlsaaPKtquQFFg.gif)

