Skip to content

Introduce Parameter Object: Bundle Parameters That Travel Together

How to replace a group of parameters that always appear together with a single object, step by step, in TypeScript, Python and PHP.

7 min readRefactoring

Introduce Parameter Object takes a group of parameters that keep showing up side by side and replaces them with one object. Callers build that object once and pass it along. Functions receive it as a single argument. The change is mechanical, but its effect is not. Once the group has a name and a type, it becomes a place to put the behaviour that was scattered across every function using those values. This guide walks through it on a cold-chain monitoring service, and shows you how to explain the move clearly in an interview.

The problem

A situation where the obvious design starts to hurt.

Picture a service that checks vaccine shipments as they arrive at a distribution hub. Every shipment has an allowed storage band, for example 2 to 8 degrees Celsius, plus a tolerance: how many minutes the cargo may spend outside that band before it has to be rejected.

The three values minCelsius, maxCelsius and toleranceMinutes are passed separately to excursionMinutes, isCompromised and shipmentReport. Every signature repeats them, and every call site has to supply them in the right order. Two adjacent numbers are easy to swap without the type checker noticing. If the team adds a fourth rule, such as a maximum single excursion, every function in the chain has to change. The concept of a storage band clearly exists in the domain, but the code has no single place that represents it.

Before and after

The same code, before and after the change.

Before

type Reading = { minute: number; celsius: number };

function excursionMinutes(readings: Reading[], minCelsius: number, maxCelsius: number): number {
  return readings.filter((r) => r.celsius < minCelsius || r.celsius > maxCelsius).length;
}

function isCompromised(
  readings: Reading[],
  minCelsius: number,
  maxCelsius: number,
  toleranceMinutes: number,
): boolean {
  return excursionMinutes(readings, minCelsius, maxCelsius) > toleranceMinutes;
}

function shipmentReport(
  shipmentId: string,
  readings: Reading[],
  minCelsius: number,
  maxCelsius: number,
  toleranceMinutes: number,
): string {
  const minutes = excursionMinutes(readings, minCelsius, maxCelsius);
  const status = isCompromised(readings, minCelsius, maxCelsius, toleranceMinutes) ? "REJECT" : "ACCEPT";
  return `${shipmentId}: ${minutes} min outside ${minCelsius}..${maxCelsius} C -> ${status}`;
}

const readings: Reading[] = [4.5, 9.1, 9.4, 7.8, 1.6].map((celsius, minute) => ({ minute, celsius }));
console.log(shipmentReport("VX-2041", readings, 2, 8, 2));
// VX-2041: 3 min outside 2..8 C -> REJECT

After

type Reading = { minute: number; celsius: number };

class StorageBand {
  constructor(
    readonly minCelsius: number,
    readonly maxCelsius: number,
    readonly toleranceMinutes: number,
  ) {}

  isOutside(celsius: number): boolean {
    return celsius < this.minCelsius || celsius > this.maxCelsius;
  }
}

function excursionMinutes(readings: Reading[], band: StorageBand): number {
  return readings.filter((r) => band.isOutside(r.celsius)).length;
}

function isCompromised(readings: Reading[], band: StorageBand): boolean {
  return excursionMinutes(readings, band) > band.toleranceMinutes;
}

function shipmentReport(shipmentId: string, readings: Reading[], band: StorageBand): string {
  const minutes = excursionMinutes(readings, band);
  const status = isCompromised(readings, band) ? "REJECT" : "ACCEPT";
  return `${shipmentId}: ${minutes} min outside ${band.minCelsius}..${band.maxCelsius} C -> ${status}`;
}

const readings: Reading[] = [4.5, 9.1, 9.4, 7.8, 1.6].map((celsius, minute) => ({ minute, celsius }));
console.log(shipmentReport("VX-2041", readings, new StorageBand(2, 8, 2)));
// VX-2041: 3 min outside 2..8 C -> REJECT

Why it's worth it

What the change buys you.

When the same values always travel together, they usually describe one thing that the code has not named yet. Here that thing is a storage band. Giving it a type does three jobs at once.

First, signatures shrink. shipmentReport goes from five parameters to three, and none of the remaining ones are interchangeable numbers that a caller could pass in the wrong order.

Second, the rule that defines the band lives in one place. The comparison celsius < min || celsius > max moves into StorageBand.isOutside. If the regulator later says the bounds are exclusive, or the team adds a maximum single excursion, you change the class instead of hunting through every function that received the loose numbers.

Third, the object becomes a natural home for further behaviour and validation. Once StorageBand exists, it is an obvious next step to reject a band whose minimum exceeds its maximum in the constructor. That is a separate, deliberate behaviour change, which is why it is not part of this refactoring.

In an interview, say it this way: I noticed a data clump, I gave it a name, and then I moved the logic that only needed those values onto the new type.

  • Shorter signatures with fewer same-typed arguments that can be silently swapped at a call site.
  • The domain concept of a storage band gets a name that readers, reviewers and tests can refer to.
  • Rules about the band live in one class, so adding a new rule no longer means editing every function in the call chain.
  • Gives you a single place to add validation, such as rejecting a band whose minimum is above its maximum, as a follow-up change.

Careful with

  • Keep the extracted logic exactly equivalent. Rewriting isOutside as !(celsius >= min && celsius <= max) looks the same but treats NaN differently: the original never counts NaN as outside, the rewrite would.
  • Make the parameter object immutable. If one function mutates a shared StorageBand, every other caller holding the same instance sees the change.
  • Do not bundle values just because they happen to sit next to each other in one signature. If they do not form a concept with a sensible name, you are only hiding a long parameter list behind a bag of fields.
  • Changing a public function signature breaks external callers. For a published API, keep the old signature as a thin wrapper that builds the object and delegates, then deprecate it.

Step by step

Small, safe steps, with the tests passing after each.

  1. Make sure the existing behaviour is covered: a test that calls shipmentReport with a known set of readings and asserts on the exact output string, plus edge cases at exactly 2 and 8 degrees.

  2. Create an empty, immutable StorageBand class with minCelsius, maxCelsius and toleranceMinutes fields. Nothing uses it yet, so tests still pass.

  3. Start with the innermost function, excursionMinutes. Add a band parameter next to the old ones, update its callers to pass a StorageBand built from the values they already have, and run the tests.

  4. Inside excursionMinutes, read the bounds from band instead of the loose parameters, then delete the now unused minCelsius and maxCelsius parameters and fix the callers. Run the tests.

  5. Repeat the same add, switch, remove cycle for isCompromised, then for shipmentReport, running the tests after each function.

  6. Update the top-level call sites to construct new StorageBand(2, 8, 2) once and pass it down. Run the tests.

  7. Move the out-of-band comparison into a StorageBand.isOutside method, keeping the exact same comparison operators, and have excursionMinutes call it. Run the tests one last time.

Smells it removes

Smells this refactoring helps remove.

Common questions

Short answers to the usual doubts.

How is this different from Preserve Whole Object?

Preserve Whole Object applies when the values already come from an existing object, so you pass that object instead of picking fields off it. Introduce Parameter Object applies when no such object exists yet and you have to create one for the clump of values.

Should I use a class, a record or a plain object type?

Anything immutable with named fields works for the first step: a TypeScript class or readonly type, a frozen Python dataclass, a PHP class with readonly properties. Prefer a real class if you expect to move behaviour onto it, as with isOutside here.

How many parameters justify a parameter object?

There is no magic number. The signal is repetition: the same two or three values appearing together in several signatures. Two values that always travel together, like a min and max, are already enough if they form one concept.

Does this refactoring change performance?

Only negligibly. You allocate one small object per call site instead of passing primitives, and access goes through a field. In a shipment check like this the cost is not measurable next to filtering the readings.

How would I describe this in a coding interview?

Point to the repeated parameter group, name the concept it represents, and explain that you will introduce the type, migrate one function at a time with tests green, then move the logic that depends only on those values into the new type.

Next guide2 of 5 · Techniques: organizing dataPreserve Whole Object: Pass the Object, Not Its FieldsRead the next one