Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will one-time usages of local variables be optimized at compile time?

Tags:

People also ask

Are local variables compile-time variables?

Local variables aren't placed anywhere at compile time. The compiler generates code that, when executed at run time, will allocate space on the stack (typically; other schemes are possible).

Is the address of a local variable known at compile-time?

Address of local variables are not known, because they reside on "stack" memory region.

What is optimized variable?

An optimization variable is a symbolic object that enables you to create expressions for the objective function and the problem constraints in terms of the variable.

Why is it good practice to use local variables whenever possible?

So, by using a local variable you decrease the dependencies between your components, i.e. you decrease the complexity of your code. You should only use global variable when you really need to share data, but each variable should always be visible in the smallest scope possible.


double calcTaxAmount() {
    double price = getA() * getB() + getC();
    double taxRate = getD() + getE();
    return price * taxRate;
}

The function above calculates the amount of tax payment.

The price and the rate are calculated by calling some other functions.

I introduced two local variables price and taxRate to just improve code readability, so both will be used only once. Will those kinds of "one-time" local variables be substituted and inlined at compile time with most modern compilers?