Code Smell 143 — Data Clumps

Code Smell 143 — Data ClumpsSome objects are always together. Why don’t we split them?TL;DR: Make cohesive primitive objects travel togetherProblemsBad CohesionDuplicated CodeValidation ComplexityReadabilityMaintainabilitySolutionsExtract ClassFind sma…


This content originally appeared on Level Up Coding - Medium and was authored by Maximiliano Contieri

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;
}

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);
}

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

Software Engineering Great Quotes

This article is part of the CodeSmell Series.

How to Find the Stinky parts of your Code


Code Smell 143 — Data Clumps was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Maximiliano Contieri


Print Share Comment Cite Upload Translate Updates
APA

Maximiliano Contieri | Sciencx (2022-06-23T20:21:14+00:00) Code Smell 143 — Data Clumps. Retrieved from https://www.scien.cx/2022/06/23/code-smell-143-data-clumps/

MLA
" » Code Smell 143 — Data Clumps." Maximiliano Contieri | Sciencx - Thursday June 23, 2022, https://www.scien.cx/2022/06/23/code-smell-143-data-clumps/
HARVARD
Maximiliano Contieri | Sciencx Thursday June 23, 2022 » Code Smell 143 — Data Clumps., viewed ,<https://www.scien.cx/2022/06/23/code-smell-143-data-clumps/>
VANCOUVER
Maximiliano Contieri | Sciencx - » Code Smell 143 — Data Clumps. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/06/23/code-smell-143-data-clumps/
CHICAGO
" » Code Smell 143 — Data Clumps." Maximiliano Contieri | Sciencx - Accessed . https://www.scien.cx/2022/06/23/code-smell-143-data-clumps/
IEEE
" » Code Smell 143 — Data Clumps." Maximiliano Contieri | Sciencx [Online]. Available: https://www.scien.cx/2022/06/23/code-smell-143-data-clumps/. [Accessed: ]
rf:citation
» Code Smell 143 — Data Clumps | Maximiliano Contieri | Sciencx | https://www.scien.cx/2022/06/23/code-smell-143-data-clumps/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.