Replace Temp with Query
- by student
The Replace Temp with Query refactoring method is recommended quite widely now but
seems to be very inefficient for very little gain.
The method from the Martin Fowler's site gives the following example:
Extract the expression into a method. Replace all references to the temp with the expression. The new method can then be used in other methods.
double basePrice = _quantity * _itemPrice;
if (basePrice > 1000)
return basePrice * 0.95;
else
return basePrice * 0.98;
becomes
if (basePrice() > 1000)
return basePrice() * 0.95;
else
return basePrice() * 0.98;
double basePrice() {
return _quantity * _itemPrice;
}
Why is this a good idea? surely it means the calculation is needlessly repeated and you have the overhead of calling a function. I know CPU cycles
are cheap but throwing them away like this seems careless?
Am I missing something?