Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How bad is redefining/shadowing a local variable?

Tags:

c++

While upgrading a legacy project to VS2015, I noticed there were a lot of errors where a local variable was redefined inside a function for example.

void fun()
{
    int count = applesCount();

    cout << "Apples cost= " << count * 1.25;

    for (int bag=0; bag<5;bag++)
    {
        int count = orangesCount(bag);

        cout << "Oranges cost = " << count * 0.75; 
    }
}

The error/warning message by compiler is:

declaration of 'count' hides previous local declaration 

I know it is obviously not a good practice to use the same name for variable count but can the compiler really mess things up as well or generally they deal this situation rather gracefully?

Is it worth it to change and fix the variable names or is unlikely to cause any harm and is low or no risk?

like image 725
zar Avatar asked Jul 22 '16 18:07

zar


People also ask

Is variable shadowing bad?

Variable shadowing is not an error syntactically. It is valid and well defined. However, if your intention was to use the variable from the outer scope, then you could consider it a logical error.

What does it mean for a local variable to shadow a field?

What does it mean for a local variable to shadow a field? A method may have a local variable with the same name as an instance field. This is called shadowing. The local variable will hide the value of the instance field. Shadowing is discouraged and local variable names should not be the same as instance field names.

How do you avoid variable shadowing in Java?

A child class can declare a variable with the same name as an inherited variable from its parent class, thus hiding the inherited variable. In other words, when the child and parent classes both have a variable with the same name, the child class' variable hides the parent class' variable.

What does shadowing mean in C++?

So what happens when we have a variable inside a nested block that has the same name as a variable in an outer block? When this happens, the nested variable “hides” the outer variable in areas where they are both in scope. This is called name hiding or shadowing.


1 Answers

I noticed there were a lot of errors where a local variable was redefined inside a function for example.

You are not demonstrating redefining here. You show an example of variable shadowing.

Variable shadowing is not an error syntactically. It is valid and well defined. However, if your intention was to use the variable from the outer scope, then you could consider it a logical error.

but can the compiler really mess things up

No.

The problem with shadowing is that it can be hard to keep track of for the programmer. It is trivial for the compiler. You can find plenty of questions on this very site, stemming from confusion caused by shadowed variables.

It is not too difficult to grok which expression uses which variable in this small function, but imagine the function being dozens of lines and several nested and sequential blocks. If the function is long enough that you cannot see all the different definitions in different scopes at a glance, you are likely to make a misinterpretation.

declaration of 'count' hides previous local declaration 

This is a somewhat useful compiler warning. You haven't run out of names, so why not give a unique name for all local variables in the function? However, there is no need to treat this warning as an error. It is merely a suggestion to improve the readability of your program.

In this particular example, you don't need the count in the outer scope after the inner scope opens, so you might as well reuse one variable for both counts.

Is it worth it to change and fix the variable names

Depends on whether you value more short term workload versus long term. Changing the code to use unique, descriptive local variable names is "extra" work now, but every time someone has to understand the program later, unnecessary shadowing will increase the mental challenge.

like image 165
eerorika Avatar answered Sep 23 '22 02:09

eerorika