Data Clumps

Groups of variables that always appear together should be turned into their own object.

Sign of Smell

If deleting one of the data values would make the others meaningless, it is a sign of data clumps. When we find identical groups of variables appearing in different parts of the code, they should be turned into their own classes to avoid data clumps.

Reason of Smell

  • Readability and Understandability: Code that uses data clumps can be difficult to read and understand, especially for developers who are not familiar with the codebase. The relationships between the data elements might not be immediately clear.

  • Duplication: When the same group of data elements is repeated across different parts of the code, it leads to unnecessary duplication. This duplication makes the code harder to maintain because if the data needs to change, it must be updated in multiple places, increasing the risk of inconsistencies.

  • Maintenance Difficulty: Updating or modifying the data becomes a maintenance challenge when it's scattered across the codebase. Any change in the data requires searching through the code to find all occurrences, which can be error-prone and time-consuming.

  • Brittleness: Since the data is spread out across multiple locations, making changes to one part of the code can inadvertently impact other parts that rely on the same data, leading to unexpected and hard-to-diagnose bugs.

Refactoring Recipe

  • Extract Class

  • Introduce Parameter Object

  • Preserve Whole Object

Reference

https://refactoring.guru/smells/data-clumps https://luzkan.github.io/smells/data-clump

Last updated