Inline Variable

Inline variable is used when a variable name does not add any additional value to an expression. If an expression is being assigned to a variable and the variable isn’t doing anything other than storing a simple expression, then there is no need for the variable. By removing unnecessary variables you simplify your code and potentially make other refactorings simpler.

Simple Example

Let’s look at a very simple example where we look at the base price for an order.

C#
double basePrice = anOrder.BasePrice;
return (basePrice > 1000.0);

This is very straightforward, but the variable is not doing anything. Its name does not add any value to the expression, so it is best to just inline the variable.

C#
return (anOrder.BasePrice > 1000.0)

References

  1. Fowler, M. et al. Refactoring: Improving the Design of Existing Code. Pearson Education, 2012. Web. Addison-Wesley Object Technology Series.
Scroll to Top