Data Clump
Symptoms
The same two or three items frequently appear together in classes and parameter lists. The code declares some fields, then methods that work with those fields, then more fields and more methods, etc. (i.e., there are groups of fields and methods together within the class). Groups of field names start or end with similar substrings.
Causes
The items are typically part of some other entity, but as yet no one has had the insight to realize that there's a missing class. Or sometimes, people know the class is missing, but think it's too small or unimportant to stand alone.
(Identifying these classes is often a major step toward simplifying a system, and it often helps you to generalize classes more easily.)
What to Do
If the items are fields in a class, use Extract Class to pull them into a new class. If the values are together in method signatures, Introduce Parameter Object to extract the new object. Look at calls that pass around the items from the new object to see if they can Preserve Whole Object instead. Look at uses of the items; there are often opportunities to use Move Method, etc., to move those uses into the new object (as you would to address the Data Class smell).
Payoff
Improves communication. May expose duplication. Usually reduces size.
Contraindications
Occasionally, passing a whole object will introduce a dependency you don't want (as lower-level classes get exposed to the whole new object instead of just its values). So you may pass in the pieces to prevent this dependency.
Very rarely, there is a measured performance problem solved by passing in the parts of the object instead of the object itself. Recognize that this is a compromise in the object model for performance. Such code is worth commenting!
 |