Add Parameter

A method needs more information from its caller.

“A method needs more information from its caller.”

Passing data down.

graph LR
    A[func()] -->|Refactoring| B[func(arg)]

When to apply (Smells)

  • Global State: Method accesses global data instead of passing it.
  • New Feature: Logic requires new input.

Motivation

  • Explicit Dependencies: Function signatures should tell you what they need to work.

Mechanics (Steps)

  1. Add the parameter to the method definition.
  2. Update all callers.

Explanation

Problem

Method calculate() needs the date.

Solution

Change it to calculate(date).

Pros and Cons

Pros Cons
  • Explicit dependencies
  • Longer parameter lists
  • Code example

    Typescript

    Before

    After

    PHP

    Before

    After