Refactoring techniqueSimplifying Method Calls
Remove Parameter
Delete a parameter the method body no longer uses, updating every caller and override so the signature states only real dependencies.
- Also known as
- Change Function Declaration
Problem
A method takes a parameter it ignores. It was used once, or added "in case", and now every caller has to compute and pass a value that changes nothing. Readers assume it matters.
Solution
Remove the unused parameter from the method and from every call site.
Before
function scheduleReminder(
appointment: Appointment,
leadMinutes: number,
timezone: string, // unused since reminders moved to UTC
channel: "sms" | "email",
): Job {
const runAt = new Date(appointment.startsAt.getTime() - leadMinutes * 60_000);
return queue.enqueue("reminder", { appointmentId: appointment.id, channel }, runAt);
}
scheduleReminder(appt, 60, clinic.timezone, "sms");After
function scheduleReminder(
appointment: Appointment,
leadMinutes: number,
channel: "sms" | "email",
): Job {
const runAt = new Date(appointment.startsAt.getTime() - leadMinutes * 60_000);
return queue.enqueue("reminder", { appointmentId: appointment.id, channel }, runAt);
}
scheduleReminder(appt, 60, "sms");Why
- A signature is a contract. Every parameter in it claims the method depends on that value.
- Callers stop computing values that are thrown away, which sometimes removes whole lookups.
Steps
- Confirm the parameter is unused in the body and in every override and implementation. A parameter unused in the base class may be used in a subclass.
- If the method is overridden or implements an interface, change all declarations together.
- If your tooling has a safe "change signature" refactoring, use it. Run the tests.
- Otherwise create a new method without the parameter, make the old one delegate to it, and move callers over a few at a time. Run the tests after each batch.
- Delete the old signature when nothing calls it.
Watch out
- Do not remove a parameter required by an interface, callback type or framework hook (Express middleware
next, event handler signatures). Prefix it with_to signal it is intentionally unused. - Positional removal in JavaScript shifts later arguments. Callers passing values by position will send the wrong value into the wrong slot without a type error if the types match. Use the type checker, and update all callers in one change.
- Public APIs need a deprecation period. Keep an overload that accepts and ignores the old argument.
- Check whether computing the argument had side effects at the call site that callers still rely on.
- The inverse technique is Add Parameter.
Related
- Inverse: Add Parameter.
- Fixes Long Parameter List, Speculative Generality and Dead Code.
- When the value is still needed but derivable, use Replace Parameter with Method Call.
Agent checklist
- The parameter is unused in every override and implementation.
- It is not required by an external interface, callback type or framework.
- All positional callers were updated in the same change.
- Argument expressions removed from call sites had no side effects callers depend on.
Treats these smells
- Long Parameter ListA function takes so many parameters that callers mix up their order and every new option changes every call site.
- Speculative GeneralityAbstractions, hooks and parameters built for future needs that never arrived make code harder to read without serving any current use.
- Dead CodeVariables, parameters, branches, methods or classes that nothing uses still sit in the codebase, costing reading time and misleading readers.
See also
- Add ParameterGive a method a new parameter when it needs data it cannot reach, migrating callers through a temporary overload or default value.
- Replace Parameter with Method CallStop passing a value the method can obtain itself; have the method call the query directly and drop the parameter.
- Rename MethodChange a method's name to say what it does, using a forwarding alias when callers cannot all be updated at once.
Original writing and examples. Topic coverage follows Refactoring.Guru, which explains this topic with its own illustrations.