I was wondering if it is good practice to declare variables just to make code (such as a long condition in an if statement) more human readable or if this wastes resources and using detailed comments is better?
Here's a simple example.
//declare variables for if statement only
int price = getProductionCost() + getTax() + getPurchaseMethod() + etc + etc;
int shipping = getLocation() + getShippingType() + etc;
if (price + shipping > 1000)
{
// Is this better practice using variables?
}
// if price + shipping > 1000
if (getProductionCost() + getTax() + getPurchaseMethod() + etc + etc + getLocation() + getShippingType() + etc > 1000)
{
// or is this option better practice with commenting and no variables?
}
I am also aware that there is a risk of the variable being modified in the same method which is a disadvantage. I tried to look for best practices on this but couldn't find anything and wasn't sure what to search for. Thank you.
I'd go for the variables to make the if-statement look more clean.
Code is read more often than it is written, but there is nothing preventing you from writing comments too. Just don't overdo it!