I've been reading Martin Fowler's Refactoring. It is generally excellent but one of Fowler's recommendations seems to be causing a little trouble.
Fowler recommends that you replace temporary variables with a query, so instead of this:
double getPrice() {
final int basePrice = _quantity * _itemPrice;
final double discountFactor;
if (basePrice > 1000) discountFactor = 0.95;
else discountFactor = 0.98;
return basePrice * discountFactor;
}
you pull out into a helper method:
double basePrice() {
return _quantity * _itemPrice;
}
double getPrice() {
final double discountFactor;
if (basePrice() > 1000) discountFactor = 0.95;
else discountFactor = 0.98;
return basePrice() * discountFactor;
}
In general I agree except that one reason I use temporary variables is when a line is too long. For example:
$host = 'https://api.twilio.com';
$uri = "$host/2010-04-01/Accounts/$accountSid/Usage/Records/AllTime";
$response = Api::makeRequest($uri);
If I tried inlining that, the line would go longer than 80 characters.
Alternately I end up with chains of code, which are themselves not that much easier to read:
$params = MustacheOptions::build(self::flattenParams($bagcheck->getParams()));
What are some strategies for reconciling the two?