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)

  1. Check if different name is better.
  2. Change the declaration.
  3. 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
  • Clarity
  • Breaking API changes (public methods)
  • Code example

    Typescript

    Before

    After

    PHP

    Before

    After