Code Smell – Bad Smell in Code : Understanding Data Clumps
The most obvious form of duplication is when you have clumps of identical code that look like some programmers went wild with the mouse, pasting the same code over and over again. These should be replaced with simple methods.
A more subtle form is the switch/case or if/else chain that appears again and again in various modules, always testing for the same set of conditions. These should be replaced with polymorphism.
Still more subtle are the modules that have similar algorithms, but that don’t share similar lines of code. This is still duplication and should be addressed by using the TEMPLATE METHOD,or STRATEGY pattern.
Often these data groups are due to poor program structure or “copypasta programming”.
If you want to make sure whether or not some data is a data clump, just delete one of the data values and see whether the other values still make sense. If this isn’t the case, this is a good sign that this group of variables should be combined into an object.
Solution
If repeating data comprises the fields of a class, use Extract Class to move the fields to their own class.
Recent Comments