Refactoring - Replace Temp with Query
You are using the temporary variable to hold the result of an expression. Extract the expression into a method. Replace all references to the temp with the new method. The new method can then be used in other methods.
Code examples :
double basePrice = _quantity * _itemPrice;
if(basePrice > 1000)
return basePrice * 0.95;
else
return basePrice * 0.98;
=> if( basePrice() > 1000)
return basePrice() * 0.95;
else
return basePrice() * 0.98;
double basePrice(){
return _quantity * _itemPrice;
}
Refatoring also includes the refactoring of the name of the variable or method. You should make it more readable and easy to understand by people.
Using the common expression words in the life will make others feel comfortable when reading your codes.
Code examples :
double basePrice = _quantity * _itemPrice;
if(basePrice > 1000)
return basePrice * 0.95;
else
return basePrice * 0.98;
=> if( basePrice() > 1000)
return basePrice() * 0.95;
else
return basePrice() * 0.98;
double basePrice(){
return _quantity * _itemPrice;
}
Refatoring also includes the refactoring of the name of the variable or method. You should make it more readable and easy to understand by people.
Using the common expression words in the life will make others feel comfortable when reading your codes.