o9s
is a microblog
home archives search feed blogroll


Data Clump

If you find that the same bits of information are always passed around together in your code, there might be a data clump at work. It’s a code smell.

If you notice this -

send_email(first_name, last_name, email);
add_to_registry(first_name, last_name, email);
remove_from_registry(first_name, last_name, email);

- then you should ask yourself -

Does first_name make sense without last_name or email?
Does last_name make sense without first_name or email?
Does email make sense without first_name or last_name?

I would answer negatively to the first two questions and positively to the last question. This tells me that first_name and last_name are a data clump.

To fix a data clump, all the items forming the data clump should be turned into a class.

class Name {
  String first_name;
  String last_name;
}

Whenever two or three values are gathered together - turn them into a $%#$%^ object.” - Martin Fowler

This might be an over-simplification. I think the class below makes as much, if not more, sense.

class Person {
  String first_name;
  String last_name;
  String email;
}

And the class below is definitely over-engineered.

class Person {
  Name name;
  String email;
}

In conclusion, know what data clumps are and fix them when they make sense to fix.

Upper limit and lower limit of ranges are very common data clumps. For example, start_date and end_date should be turned into date_range.

Posted on 2018-01-25   #codesmell  






← Next post    ·    Previous post →