Preserve Whole Object

You are getting several values from an object and passing them as parameters to a method.

“Send the whole object instead.”

Don’t unpack unless necessary.

graph LR
    A[func(low, high)] -->|Refactoring| B[func(range)]

When to apply (Smells)

  • Long Parameter List: Passing 5 properties of the same object.
  • Data Clumps: Arguments that always appear together.

Motivation

  • Future proofing: If the method needs more data from the object later, you don’t need to change the signature.
  • Readability: plan.withinRange(range) vs plan.withinRange(low, high).

Mechanics (Steps)

  1. Change parameter to accept the whole object.
  2. Update method body to access properties from the object.

Explanation

Problem

val = plan.withinRange(daysTempRange.getLow(), daysTempRange.getHigh())

Solution

val = plan.withinRange(daysTempRange)

Pros and Cons

Pros Cons
  • Shorter signature
  • Dependency on the whole object (Coupling)
  • Code example

    Typescript

    Before

    After

    PHP

    Before

    After