重新定义/隐藏局部变量有多糟糕? [英] How bad is redefining/shadowing a local variable?

查看:37
本文介绍了重新定义/隐藏局部变量有多糟糕?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将旧项目升级到VS2015时,我注意到在函数中重新定义局部变量时会发生很多错误.

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 

我知道对变量 count 使用相同的名称显然不是一个好习惯,但是编译器是否真的可以搞乱事情,或者通常它们会比较优雅地处理这种情况?

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?

推荐答案

例如,在函数内部重新定义局部变量时,我注意到了很多错误.

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

您不是要在此处演示重新定义.您将显示一个变量 shadowing 的示例.

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

否.

阴影的问题在于,可能很难跟踪对于程序员.对于编译器而言,这是微不足道的.由于阴影变量引起的混乱,您可以在此站点上找到很多问题.

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.

在此特定示例中,在内部范围打开之后,您无需在外部范围中使用 count ,因此您最好将一个变量重用于两个计数.

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.

这篇关于重新定义/隐藏局部变量有多糟糕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆