Programming Search⌘K

Patterncore

Data Clumps

Also called data clump.

A code smell: the same group of values travels together through many fields and parameter lists, but the program has no type that names the group or holds the rules about it.

Family
code-smell
Intent
Find the values that always appear together, give the group a type of its own, and then move the behaviour that uses the group into that type.

Intent

Find the values that the code always passes and stores together, and give the group a type with a name. Then move the rules about the group, such as checks and comparisons, into that type, so that each rule is written once.

Problem

Some values only make sense as a group. A start date and an end date are a period. A street, a city and a postcode are an address. A host and a port are an endpoint. When the program has no type for the group, the values travel one by one: two fields in each class that needs them, and two parameters in each function that uses them.

Fowler states the fix as a joke in the bliki entry: "Whenever two or three values are gathered together - turn them into a $%#$%^ object."1 He describes how to see the smell: "You spot it when you constantly see the same few data items passed around together", and gives the example that "start and end are a good example of a data clump wanting to be a range."1

The forces are these:

  • Each value has a type already, such as a number, a string or a date, so the code compiles and runs without a type for the group.
  • The rules about the group have no home. The rule that the start comes before the end, or the test that two periods overlap, is written again in each function that needs it.
  • Parameter lists grow. Two periods are four parameters, and a call with two of them in the wrong order is still valid code.
  • Fowler notes that "Often data clumps are primitive values that nobody thinks to turn into an object."1 This links the smell to Primitive Obsession.

Refactoring gives a test for a clump: delete one of the values, and ask if the others still make sense. If they do not, the values are one thing, and the program needs an object for it.2

Solution

Refactoring gives the order of the steps.2 The catalogue at refactoring.com has a card for each refactoring.3

  1. The values are fields of a class: Extract Class to move them into a class of their own.
  2. The values are parameters of functions: Introduce Parameter Object, to pass one object in place of the group.
  3. The caller already has an object that holds the values: Preserve Whole Object, to pass that object in place of the values taken out of it.

The first edition of Refactoring gives the same three refactorings for this smell.4

Fowler says that the first benefit is shorter parameter lists, and that "The interesting stuff happens as you begin to look for behavior to move into the new objects."1 Refactoring makes the same point: once the class exists, look for Feature Envy, the functions that work on the values, and move them into it.2

The type does not need to use all of its fields in each place. Refactoring says not to worry about a function that uses only some of the fields of the object, as long as two or more fields are replaced.2

Example

Two functions take a period as two day numbers. Each one checks the order again, and a call with the two pairs mixed up gives a wrong answer and no error. After the change, the period is a DateRange that checks its own order once, and the two rules are its methods.

import assert from "node:assert/strict";

// Before: each period is two loose numbers.
function overlapsBefore(aStart, aEnd, bStart, bEnd) {
  return aStart <= bEnd && bStart <= aEnd;
}
function containsBefore(start, end, day) {
  return start <= day && day <= end;
}

assert.equal(overlapsBefore(1, 5, 4, 9), true);
assert.equal(overlapsBefore(1, 4, 5, 9), false);
assert.equal(overlapsBefore(1, 5, 9, 4), false); // the second period is reversed, and no one is told
console.log("before: a reversed period gives a wrong answer");

// After: the clump is a type, and the rules live in it.
class DateRange {
  constructor(start, end) {
    if (end < start) throw new RangeError(`range ${start}..${end} ends before it starts`);
    this.start = start;
    this.end = end;
  }
  contains(day) {
    return this.start <= day && day <= this.end;
  }
  overlaps(other) {
    return this.start <= other.end && other.start <= this.end;
  }
}

const week = new DateRange(1, 5);
assert.equal(week.overlaps(new DateRange(4, 9)), true);
assert.equal(week.overlaps(new DateRange(6, 9)), false);
assert.equal(week.contains(5), true);
assert.equal(week.contains(6), false);
assert.equal(containsBefore(1, 5, 5), week.contains(5));
assert.throws(() => new DateRange(9, 4), RangeError);
console.log("after: tests pass");
Output
before: a reversed period gives a wrong answer
after: tests pass

The second version has one place for the rule that a period does not end before it starts. A reader of week.overlaps(other) sees two periods, not four numbers. When a third rule is needed, such as the length of a period, it has an obvious home.

Language What the example shows
JavaScript a class with a checking constructor, as above
Python a frozen dataclass with a __post_init__ check
Java a record with a compact constructor that checks the order
Go a struct with a constructor function that returns an error
PythonGitHub
from dataclasses import dataclass


# Before: each period is two loose numbers.
def overlaps_before(a_start, a_end, b_start, b_end):
    return a_start <= b_end and b_start <= a_end


def contains_before(start, end, day):
    return start <= day <= end


assert overlaps_before(1, 5, 4, 9)
assert not overlaps_before(1, 4, 5, 9)
assert not overlaps_before(1, 5, 9, 4)  # the second period is reversed, and no one is told
print("before: a reversed period gives a wrong answer")


# After: the clump is a type, and the rules live in it.
@dataclass(frozen=True)
class DateRange:
    start: int
    end: int

    def __post_init__(self):
        if self.end < self.start:
            raise ValueError(f"range {self.start}..{self.end} ends before it starts")

    def contains(self, day):
        return self.start <= day <= self.end

    def overlaps(self, other):
        return self.start <= other.end and other.start <= self.end


week = DateRange(1, 5)
assert week.overlaps(DateRange(4, 9))
assert not week.overlaps(DateRange(6, 9))
assert week.contains(5)
assert not week.contains(6)
assert contains_before(1, 5, 5) == week.contains(5)
assert week == DateRange(1, 5)
try:
    DateRange(9, 4)
except ValueError:
    pass
else:
    raise AssertionError("a reversed range was accepted")
print("after: tests pass")
Output
before: a reversed period gives a wrong answer
after: tests pass
  • frozen=True makes the fields read-only after construction, so a range that passed the check cannot be changed into one that fails it.
  • The dataclass writes __eq__ from the fields, so two ranges with the same start and end are equal. That is the behaviour of a value.
JavaGitHub
public class Main {
    // Before: each period is two loose numbers.
    static boolean overlapsBefore(int aStart, int aEnd, int bStart, int bEnd) {
        return aStart <= bEnd && bStart <= aEnd;
    }

    static boolean containsBefore(int start, int end, int day) {
        return start <= day && day <= end;
    }

    // After: the clump is a type, and the rules live in it.
    record DateRange(int start, int end) {
        DateRange {
            if (end < start) {
                throw new IllegalArgumentException("range " + start + ".." + end + " ends before it starts");
            }
        }

        boolean contains(int day) {
            return start <= day && day <= end;
        }

        boolean overlaps(DateRange other) {
            return start <= other.end && other.start <= end;
        }
    }

    static void check(boolean ok, String what) {
        if (!ok) throw new AssertionError(what);
    }

    public static void main(String[] args) {
        check(overlapsBefore(1, 5, 4, 9), "before overlap");
        check(!overlapsBefore(1, 4, 5, 9), "before gap");
        check(!overlapsBefore(1, 5, 9, 4), "before reversed"); // no one is told
        System.out.println("before: a reversed period gives a wrong answer");

        DateRange week = new DateRange(1, 5);
        check(week.overlaps(new DateRange(4, 9)), "overlap");
        check(!week.overlaps(new DateRange(6, 9)), "gap");
        check(week.contains(5) && !week.contains(6), "contains");
        check(containsBefore(1, 5, 5) == week.contains(5), "same result");
        check(week.equals(new DateRange(1, 5)), "equal by value");
        try {
            new DateRange(9, 4);
            throw new AssertionError("a reversed range was accepted");
        } catch (IllegalArgumentException expected) {
            // the check in the compact constructor
        }
        System.out.println("after: tests pass");
    }
}
Output
before: a reversed period gives a wrong answer
after: tests pass
  • The compact constructor DateRange { ... } runs before the fields are set, so it is the one place to reject a reversed range.
  • A record gets equals, hashCode and the accessors start() and end() from its components.
package main

import "fmt"

// Before: each period is two loose numbers.
func overlapsBefore(aStart, aEnd, bStart, bEnd int) bool {
	return aStart <= bEnd && bStart <= aEnd
}

func containsBefore(start, end, day int) bool {
	return start <= day && day <= end
}

// After: the clump is a type, and the rules live in it.
type DateRange struct {
	start, end int
}

func NewDateRange(start, end int) (DateRange, error) {
	if end < start {
		return DateRange{}, fmt.Errorf("range %d..%d ends before it starts", start, end)
	}
	return DateRange{start, end}, nil
}

func (r DateRange) Contains(day int) bool {
	return r.start <= day && day <= r.end
}

func (r DateRange) Overlaps(other DateRange) bool {
	return r.start <= other.end && other.start <= r.end
}

func mustRange(start, end int) DateRange {
	r, err := NewDateRange(start, end)
	if err != nil {
		panic(err)
	}
	return r
}

func check(ok bool, what string) {
	if !ok {
		panic(what)
	}
}

func main() {
	check(overlapsBefore(1, 5, 4, 9), "before overlap")
	check(!overlapsBefore(1, 4, 5, 9), "before gap")
	check(!overlapsBefore(1, 5, 9, 4), "before reversed") // no one is told
	fmt.Println("before: a reversed period gives a wrong answer")

	week := mustRange(1, 5)
	check(week.Overlaps(mustRange(4, 9)), "overlap")
	check(!week.Overlaps(mustRange(6, 9)), "gap")
	check(week.Contains(5) && !week.Contains(6), "contains")
	check(containsBefore(1, 5, 5) == week.Contains(5), "same result")
	check(week == mustRange(1, 5), "equal by value")
	_, err := NewDateRange(9, 4)
	check(err != nil, "a reversed range was accepted")
	fmt.Println("after: tests pass")
}
Output
before: a reversed period gives a wrong answer
after: tests pass
  • The fields are unexported, so code in other packages must call NewDateRange, which checks the order. Code in the same package can still write DateRange{9, 4}.
  • A struct whose fields are all comparable can be compared with ==, so two ranges with the same fields are equal.

Consequences

The group has a name, and the name is in the signatures. Functions that take the group have shorter parameter lists. The rules about the group are written once and tested once. A value of the type is valid by construction, so the functions that receive it do not need to check it again.

The cost is one more type, and a conversion at the edges of the program. Code that reads two columns from a database or two fields from a request must build the object, and code that writes them must take it apart again.

Not every pair of values that appears together is a clump. The test from Refactoring helps: if one value still makes sense without the other, the two are separate values that happen to be used in the same place.2 The x and y of a point fail the test, so they are a clump. A user name and a page size in the same request pass it, so they are not.

How to fix it

Long Parameter List. A data clump in the parameters is one of the common causes of a long list. See Long Parameter List.

Primitive Obsession. A group of primitive values with no type is a primitive obsession for a compound value. See Primitive Obsession.

Value objects. The type that replaces a clump is often a small immutable value, such as a range or an amount of money, compared by its fields. See Value object.

References

  1. Martin Fowler, Data Clump, bliki, 5 January 2006, the whole entry
  2. Martin Fowler, with Kent Beck, Refactoring, Improving the Design of Existing Code, second edition, Addison-Wesley, 2018, Chapter 3, Bad Smells in Code, Data Clumps
  3. Martin Fowler, Catalog of Refactorings, the cards Extract Class, Introduce Parameter Object and Preserve Whole Object
  4. Martin Fowler, Refactoring, Improving the Design of Existing Code, Addison-Wesley, 1999, Chapter 3, Bad Smells in Code, Data Clumps

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW4 complete
  • a review of the text: no review record reviews/ED0EC0/text.yaml