DEV Community

Cover image for Code Smell 143 - Data Clumps
Maxi Contieri
Maxi Contieri

Posted on • Originally published at maximilianocontieri.com

5 2

Code Smell 143 - Data Clumps

Some objects are always together. Why don't we split them?

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.

Sample Code

Wrong

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;
}
Enter fullscreen mode Exit fullscreen mode

Right

public class TimeInterval
{
    public TimeInterval(DateTime from, DateTime tol)
    {
        // We shoud validate From < To
        From = from;
        To = to;
    }
}

public DinnerTable(Person guest, DateTime from, DateTime to)
{    
    Guest = guest;
    Interval = new TimeInterval(from, to);
}
Enter fullscreen mode Exit fullscreen mode

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 hid the primitive data.

Relations

More Info

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


This article is part of the CodeSmell Series.

SurveyJS custom survey software

Simplify data collection in your JS app with a fully integrated form management platform. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more. Integrates with any backend system, giving you full control over your data and no user limits.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay