Rename Method
The name of a method does not explain what the method does.
“A method name does not reveal its purpose.”
Names matter.
graph LR
A[String::getNm()] -->|Refactoring| B[String::getName()]
When to apply (Smells)
- Mysterious Name: Methods named
calc,doIt,process. - Misleading Name:
getPerson()that actually creates a person.
Motivation
- Communication: Code is for humans. Names are the primary way to communicate intent.
Mechanics (Steps)
- Check if different name is better.
- Change the declaration.
- Find all callers and update them.
Explanation
Problem
Name is bad. cust.p()
Solution
Rename it. cust.printPrice()
Real world analogy
If you have a button labeled “Push Me” that launches a missile, rename it to “Launch Missile”.
Pros and Cons
| Pros | Cons |
|---|---|
Code example
Typescript
Before
After
PHP
Before
After