Rename Method
Before you begin: Methods, arguments, and call sites.
Rename Method changes a method’s name to communicate its purpose more clearly. Its parameters, calculation, and result need not change. The improvement appears wherever the method is called. 1
Original version: calculate what?
public static class Rectangles
{
public static int Calculate(int width, int height)
{
return width * height;
}
}int result = Rectangles.Calculate(3, 4);
Console.WriteLine(result);Calculate says that work occurs, but not whether it calculates area, perimeter, or something else.
def calculate(width, height):
return width * heightresult = calculate(3, 4)
print(result)The implementation reveals the meaning, but every caller should not need to open it to identify the result.
Refactored version: name the result
public static class Rectangles
{
public static int Area(int width, int height)
{
return width * height;
}
}int area = Rectangles.Area(3, 4);
Console.WriteLine(area); // 12The call now identifies the quantity. The multiplication is unchanged.
def area(width, height):
return width * heightrectangle_area = area(3, 4)
print(rectangle_area) # 12The name explains what the function returns before the reader sees its body.
Rename the contract everywhere it is used
An editor’s symbol-aware rename is safer than an unrestricted text replacement. Still inspect reflection, string-based callbacks, routes, serialization metadata, and external users that the editor may not recognize.
For a public API, retain an old-name forwarding method during a migration when consumers cannot all update together. Mark the compatibility path clearly rather than maintaining two independent implementations.
Choose names from the caller’s perspective. Include units when ambiguity matters, and make side effects visible: a method that saves data should not sound like a harmless getter. Do not rename a method to promise behavior it does not provide; changing the implementation to match a new promise is a separate change.
The test here is intentionally simple: both names calculate 12 for a three-by-four rectangle. The harder review question is whether the new name makes a real call easier to understand.
Sources
- Martin Fowler — Change Function Declaration, including Rename Method.