# Code Smell 143 - Data Clumps

> TL;DR: Make cohesive primitive objects travel together

# Problems

*   Bad Cohesion
    
*   Duplicated Code
    
*   Validation Complexity
    
*   Readability
    
*   Maintainability
    

# Solutions

1.  Extract Class
    
2.  Find small objects
    

# Context

This smell is friends with primitive obsession.

If two or more primitive objects are glued together, with business logic repeated and rules between them, we need to find the existing concept on the [bijection](https://maximilianocontieri.com/the-one-and-only-software-design-principle).

# Sample Code

## Wrong

```csharp
public class DinnerTable
{
    public DinnerTable(Person guest, DateTime from, DateTime to)
    {
        Guest = guest; 
        From = from;
        To = to;
    }
    private Person Guest;
    private DateTime From; 
    private DateTime To;
}
```

## Right

```csharp
public class TimeInterval
{
    public TimeInterval(DateTime from, DateTime to)
    {
        // We should validate From < To
        From = from;
        To = to;
    }
}

public DinnerTable(Person guest, DateTime from, DateTime to)
{    
    Guest = guest;
    Interval = new TimeInterval(from, to);
}
```

# Detection

\[X\] Semi-Automatic

Detection based on cohesion patterns is available o a few linters.

# Tags

*   Cohesion
    

# Conclusion

Group behavior in the right place and hide the primitive data.

# Relations

%[https://maximilianocontieri.com/code-smell-122-primitive-obsession] 

%[https://maximilianocontieri.com/code-smell-01-anemic-models] 

%[https://maximilianocontieri.com/code-smell-27-associative-arrays] 

# More Info

*   [Refactoring Guru](https://refactoring.guru/es/smells/data-clumps)
    
*   [Wikipedia](https://en.wikipedia.org/wiki/Data_clump)
    

# Credits

Photo by Dynamic Wang on Unsplash

* * *

> The heart of the software is its ability to solve domain-related problems for its user. All other features, vital though they may be, support this basic purpose.

*Eric Evans*

%[https://maximilianocontieri.com/software-engineering-great-quotes] 

* * *

This article is part of the CodeSmell Series.

%[https://maximilianocontieri.com/how-to-find-the-stinky-parts-of-your-code]
