- By Matt Belcher
- ·
- Posted 09 Jun 2020
There are models to describe the process of learning as Dreyfus model or Shuhari, but I'd like to explain another process when learning a new technical skill. It is composed of 2 phases:
Fascination. This is when we learn a new concept that sounds really good to solve our problems, and we apply it in any place we have an opportunity. We are so convinced about its advantages that it seems we have a radar to detect a place in which we can apply it. It's like the first stage at Shuhari, but we are absolutely fascinated and we don't believe in another world.
Questioning. This is when we have assimilated that concept and we are able to decide where it's convenient to implement it. We have learnt from the previous stage and now we are moderate and reasonable about our decisions.
But sometimes it's really difficult to jump to the second stage, because we are continuously receiving messages about a concept for years. It's the case of no duplication.
This week, I was reviewing a test class with 4 extracted parameterized methods (for 4 different types of expectations) and 6 extracted methods (for 6 different JSON files). I sat with the author of that class and we applied inline method refactoring. After that, we realized that the test class was more readable and understandable without those 10 additional methods, despite having duplicated source code.
A few weeks ago, I helped to separate two different modules. They were together, because they had some similar domain objects, although they didn't use the same properties. It was really difficult to know which properties and methods needed each module.
What's the reason of those extracted methods or that reuse?:
Alerts: duplicated lines (%) > 10%
One of the metrics we can find in static code analysis tools is the percentage of duplicated lines. Managers need metrics and this one is usually included as a guarantee of quality code. However, striving for a certain number of duplicated lines in a board can cause these consequences:
We tend to associate this metric with D.R.Y. Principle (Don't Repeat Yourself), but let's read the principle carefully:
Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.
This principle is talking about knowledge, not duplicated lines. Two pieces of code could be apparently similar, but have completely different reasons to change, because they represent different pieces of knowledge. Therefore, removing that duplication can muddle the logic and make any future change worse.
That's the reason why I usually highlight knowledge when explaining the third rule of simple design:
No knowledge duplication
To sum up, some recommendations:
DRY makes sense when it reduces the cost of change more than it increases the cost of understanding the code.
Thanks:
Software is our passion.
We are software craftspeople. We build well-crafted software for our clients, we help developers to get better at their craft through training, coaching and mentoring, and we help companies get better at delivering software.