I think there are two key factors here:
- How complex is the expression?
- How meaningful is the intermediate variable?
Take an example where we have three elements to get a final price:
total = price + tax - discount;
An intermediate variable used only once is probably a bad idea:
taxed_price = price + tax;
total = taxed_price - discount;
But if the "taxed_price" variable is actually used repeatedly in the function, then naming and reusing it might make sense.
Meanwhile, if the individual elements are all themselves expressions, the full expression becomes unwieldy inline:
total = product.get_price() + calculate_tax(jurisdiction, product) - session.get_active_discount();
In this case, there are few enough operators that we can put a line break next to each one, which helps, but it's still hard to see at a glance what the overall calculation is doing
total = product.get_price()
+ calculate_tax(jurisdiction, product)
- session.get_active_discount();
In this case, introducing single-use variables makes it easier to read:
price = product.get_price();
tax = calculate_tax(jurisdiction, product);
discount = session.get_active_discount();
total = price + tax - discount;
But the variables are chosen to be meaningful, not just arbitrary break points - this doesn't really help, if taxed_price
is only used once:
taxed_price = product.get_price() - calculate_tax(jurisdiction, product);
discount = session.get_active_discount();
total = taxed_price - discount;
And this is almost certainly nonsensical (it's hard to even name the variable, which is always a bad smell):
price = product.get_price();
tax_minus_discount = calculate_tax(jurisdiction, product) - session.get_active_discount();
total = price + tax_minus_discount;