变量声明的优化与问候范围 [英] Optimal declaration of variables with regards to scope

查看:117
本文介绍了变量声明的优化与问候范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我问这个问题主要是在关于C编程,但在任何语言的见解,欢迎选购。

I ask this question mostly in regards to C programming, but insights on any language are welcome.

当涉及到C,我知道它只是让变量的声明出现在code块的开始。我一直在IM pression,一要申报将在函数的最开始在函数中使用的所有变量之下。但也有许多场合我有一个只有一个循环内使用的变量(或类似的块)。

When it comes to C, I know it only lets variable declarations occur at the very beginning of a block of code. And I have been under the impression that one should declare all variables to be used within a function at the very beginning of the function. But there are many occasions where I'll have a variable that is only used within a loop (or similar block).

一个例子是对某些返回值的临时变量:

an example would be a temporary variable for some return value:

while ( whatever ) {
  int ret;
  ret = getSomeValue();
}

或在某些国家可能需要召开方式:

or where some state may need to be held:

while ( whatever ) {
  static int count=0;
  count++;
}

我在想,如果它被认为是不正确的,或者,如果有控制流块内声明变量的负面影响,如if-else语句,for循环,while循环,等等。

I was wondering if it is considered improper, or, if there is any negative impact to declaring variables within control flow blocks such as if-else, for loops, while loops, etc.

如果变量始终与最紧密的范围内声明?关于静态声明什么呢?

Should variables always be declared with the tightest possible scope? what about for static declaration?

修改
好吧,我大概应该说我知道C99是更自由,当涉及到您声明变量,但是从很多C code口看到,他们还在通常是在顶部声明。另外,我用VS2K8仍抱怨声明。

Edit: Okay, I probably should have said I'm aware C99 is more liberal when it comes to where you declare variables, but from a lot of C code I see, they're still usually declared at top. Also, I use VS2K8 which still complains about declarations.

此外,看到我有2票关闭这个THEAD,我会说清楚,我更关心的是性能和编译器方面比我有什么风格。

Also, seeing as I've had 2 votes to close this thead, I'll make it clear that I'm more concerned with the performance and compiler aspects than I am with anything stylistic.

推荐答案

好吧,大约在块顶部声明的一点是C89和K&安培; R,但由于C99你可以混合声明和陈述。风格依然存在,可能是很好的理由,但它不是必需的。

Ok, the thing about declarations at the top of blocks is C89 and K&R, but since C99 you can mix declarations and statements. The style persists, probably for good reasons, but it's not required.

有一段时间,如果你声明的很多局部变量权在那里他们被使用,因为它取得了较好的寄存器分配的一些编译器会做得更好。 pre-C99,你不得不在块顶部声明,或用厂商扩展,但你仍然可以决定实际上使用的只有$ C $的局部区域​​中的每个变量角

There was a time when some compilers would do a better job if you declared lots of local variables right where they were used, because it made register allocation better. Pre-C99, you had to declare them at the top of blocks or use a vendor-extension, but you could still decide to actually use each variable only in a local area of code.

这可能似乎在一个点上重要的直接支持这种更灵活的声明的位置,但我怀疑这几天优化不需要这种帮助。

It probably seemed important at one point to directly support this with more flexible declaration placement, but I suspect that these days the optimizers don't need that kind of help.

某些编码标准需要在函数的顶部声明。我不知道如果是这样的话,你可以找到他们,如果它只是从那里没有做的遗留下来可能会限制向后的便携性。

Some coding standards require declarations at the top of functions. I don't know if this is so you can find them or if it's just left over from the days where not doing that might limit backwards portability.

在一般情况下,更正式的标准,少它喜欢交织声明和宣言,除循环控制变量。就个人而言,我不认为它存在的问题。

In general, the more formal the standard, the less it likes interleaving statements and declarations, except for loop control variables. Personally, I don't see the problem with it.

混合声明的优点:局部性。适合编译器,有利于读者。什么可能的益处可以从在一个函数的顶部声明一个循环变量累积?重用了网页一个变量的不相关的目的似乎是在可能的不必要的互动方面混乱和不明智的。

Advantages of mixed declarations: locality. Good for the compiler, good for the reader. What possible benefit can accrue from declaring a loop variable at the top of a function? Reusing a variable down the page for an unrelated purpose seems confusing and unwise in terms of possible unwanted interactions.

分组声明的优点:code看起来比较清爽的主观的方式,你知道去哪里找每一个声明,如果你的函数的顶部过于远离变量的使用,也许这就是告诉你,你需要从中提取显然过于大的第二或第三的功能。

Advantages of grouped declarations: the code looks cleaner in a subjective way, you know where to go to find every declaration, and if the top of your function is too far away from the use of the variable, perhaps that's telling you that you need to extract a second or third function from the obviously too-big one.

这篇关于变量声明的优化与问候范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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