Course contentsShow
← Software Engineering
Lesson 347 of 1,93310. RefactoringFree lesson

Inline Method and Inline Variable

Discover when to remove unnecessary indirection by inlining methods or variables that don't add clarity.

Inline Method and Inline Variable

What you'll learn: You'll discover when removing unnecessary intermediate methods or variables makes your code clearer instead of more obscure.

The Core Idea

Not all indirection improves code. Sometimes a method does so little that its body is as clear as its name—or a variable just stores a value without adding meaningful context. In these cases, inlining (replacing the method call with its body, or replacing the variable reference with its value) removes clutter and improves readability.

Think of it like giving directions: saying "turn left at Main Street" is helpful, but saying "go to the corner, then execute the left-turn maneuver" adds unnecessary ceremony when the action is already obvious.

When to Inline a Method

Inline a method when:

  • The method body is as clear as its name
  • The method is only called once and doesn't need the abstraction
  • You're about to refactor and want to consolidate before extracting differently

Example concept:

// Before: unnecessary indirection
function getDiscountedPrice() {
    return getBasePrice();
}

// After: inline the method
function getDiscountedPrice() {
    return product.price * quantity;
}

When to Inline a Variable

Inline a variable when it doesn't make the expression clearer:

// Before: variable adds no value
const basePrice = order.basePrice();
return basePrice > 1000;

// After: inline the variable
return order.basePrice() > 1000;

The Balance

Inlining is the opposite of Extract Method. Use it when simplification beats abstraction—when the indirection obscures rather than clarifies intent.

Key Takeaway: Inline methods or variables that don't earn their keep—if the indirection doesn't add clarity or flexibility, remove it to reveal the underlying logic directly.