Inline Method

Inline method is the opposite of extract method. Sometimes a method is a single line of code, and the code is just as clear as the name. Indirection does solve many problems in programming, but needless indirection can be confusing if not irritating.

Simple Example

Here is a simple method that determines a library patron’s rating:

C#
private int GetRating() {
  return (MoreThanThreeLateReturns() ? 0 : 1;
}

private bool MoreThanThreeLateReturns() {
  return this._numberOfLateReturns > 3;
}

This code is extremely simple, but the method MoreThanThreeLateReturns is a single line of code and the name of the method does not add any value—the expression is as obvious as the name of the method. In this example, the method MoreThanThreeLateReturns does not add any value and should be inlined.

C#
private int GetRating() {
  return (this._numberOfLateReturns > 3 ? 0 : 1;
}

References

  • Fowler M, Beck K, Brant J, Opdyke W, Roberts D. Refactoring: Improving the Design of Existing Code. Addison-Wesley; 1999.
Scroll to Top