A logbook for curious waters
← Software Engineering Field Guide

Change Function Declaration

Before you begin: Function declarations, parameters, arguments, and units.

Change Function Declaration changes a function’s name, parameters, or both. It includes smaller refactorings such as Rename Method, Add Parameter, and Remove Parameter. The declaration is what callers see first, so improving it can clarify many lines of code at once. 1

Original version: an ambiguous name and argument order

A movement calculation returns whole meters per second. For this example, elapsed time is positive and distance is an exact multiple of time.

public static class Motion
{
    public static int Get(int seconds, int meters)
    {
        return meters / seconds;
    }
}
Console.WriteLine(Motion.Get(2, 10)); // 5
def get(seconds, meters):
    return meters // seconds
print(get(2, 10))  # 5

The call says little about the returned quantity. Both arguments are integers, so reversing them would remain valid syntax while calculating something different.

Refactored version: name the result and align the inputs

public static class Motion
{
    public static int MetersPerSecond(int meters, int seconds)
    {
        return meters / seconds;
    }
}
Console.WriteLine(Motion.MetersPerSecond(meters: 10, seconds: 2)); // 5

The operation now names its output unit. The parameters follow the calculation’s distance-then-time order, and the updated caller uses named arguments to make their meanings unmistakable.

def meters_per_second(meters, seconds):
    return meters // seconds
print(meters_per_second(meters=10, seconds=2))  # 5

The new name and named arguments remove the old guessing. The division and its result remain unchanged because the caller was migrated correctly.

Change both sides of the contract

A declaration and its callers must agree. Renaming the function while forgetting to reorder an old positional call can produce a plausible but wrong answer that the compiler will not catch when both arguments have the same type.

For a widely used API, introduce the new declaration and make the old operation forward to it temporarily. Migrate consumers, then remove the compatibility entry point when it is safe. Also inspect callbacks, reflection, and external clients that a local symbol search may miss.

This example retains integer division and the same input assumptions. Changing the return type to a fractional speed, validating zero time, or changing rounding would be additional contract changes. A clearer signature should help reveal those decisions, not smuggle them into a mechanical rename.

Sources

  1. Martin Fowler — Change Function Declaration.