Inline Method: Removing Unnecessary Indirection
You'll learn when to eliminate a wrapper method whose body is just as clear (or clearer) than its name.
The Core Idea
Sometimes we create methods to give a descriptive name to a chunk of logic. But occasionally, the method body becomes so simple that the name adds no real clarity—it just adds an extra step for readers to follow. Inline Method is the reverse of Extract Method: you take the body of a trivial method and put it directly into the calling code, removing the middleman.
Think of it like asking someone, "Could you hand me that book?" when you could just reach over and grab it yourself. If the book is right next to you, the extra request is unnecessary indirection.
When to Inline a Method
Use this refactoring when:
- The method body is as clear as its name. If
getTotalPrice()just returnsprice * quantity, calling code might be clearer with the expression directly. - You have too many tiny helper methods that fragment logic without adding understanding.
- The abstraction provides no real value. If the method doesn't hide complexity or enable reuse, it's overhead.
Before Inline Method:
def isOverBudget(project):
return checkBudgetStatus(project)
def checkBudgetStatus(project):
return project.cost > project.budget
After Inline Method:
def isOverBudget(project):
return project.cost > project.budget
The intermediate checkBudgetStatus() added no clarity—it just repeated the intent already expressed by isOverBudget().
Balance with Extract Method
Remember Extract Method from the previous lesson? Inline Method is its counterpart. You extract when complexity needs hiding; you inline when indirection obscures simplicity. Both are tools to keep code readable.
Key Takeaway: Inline a method when its body is clearer than the layer of indirection it creates, reducing unnecessary steps for readers to follow.