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)
- Add the parameter to the method definition.
- Update all callers.
Explanation
Problem
Method calculate() needs the date.
Solution
Change it to calculate(date).
Pros and Cons
| Pros | Cons |
|---|---|
Code example
Typescript
Before
After
PHP
Before
After