变量声明 [英] Declaration of variables

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

问题描述

我一直认为你应该只根据需要声明变量,

并且隐含地似乎很多作者鼓励它,但另一个

一天朋友告诉我在循环中声明变量并不好

练习,例如:


for(int i = 0; i< size-1; + + i){

int aux = array [i];

array [i] = array [i + 1];

array [ i + 1] = aux;

}


应该写成:


int aux;

for(int i = 0; i< size-1; ++ i){

aux = array [i];

array [i ] = array [i + 1];

array [i + 1] = aux;

}


你是什么认为? Thaks!

I have always felt that you should only declared variables as needed,
and implicitily it seems many authors to encourage it, but the other
day a friend told me that declaring variables inside a loop wasn''t good
practice, something like:

for(int i=0; i<size-1; ++i){
int aux = array[i];
array[i]=array[i+1];
array[i+1]=aux;
}

should be written like:

int aux;
for(int i=0; i<size-1; ++i){
aux = array[i];
array[i]=array[i+1];
array[i+1]=aux;
}

What do you think? Thaks!

推荐答案

Gaijinco写道:
Gaijinco wrote:
我一直认为你应该只根据需要声明变量,
并且隐含地似乎很多作者都鼓励它,但另一天的朋友告诉我,在循环中声明变量并不是很好的实践,例如:

for(int i = 0; i< size-1; ++ i){
int aux = array [i];
array [i] = array [i + 1] ;
数组[i + 1] = aux;
}
应该写成:

int aux;
for(int i = 0; i< size-1; ++ i){
aux = array [i];
array [i] = array [i + 1];
array [i + 1] = aux;
}

你怎么看? Thaks!
I have always felt that you should only declared variables as needed,
and implicitily it seems many authors to encourage it, but the other
day a friend told me that declaring variables inside a loop wasn''t good
practice, something like:

for(int i=0; i<size-1; ++i){
int aux = array[i];
array[i]=array[i+1];
array[i+1]=aux;
}

should be written like:

int aux;
for(int i=0; i<size-1; ++i){
aux = array[i];
array[i]=array[i+1];
array[i+1]=aux;
}

What do you think? Thaks!




我认为这是你的朋友,应该向你证明他的方法有一些优势。

V

-

请在邮寄回复时从我的地址中删除资金



I think it''s your friend who should _prove_ it to you that there is some
advantage to his method.

V
--
Please remove capital As from my address when replying by mail


Gaijinco写道:
Gaijinco wrote:
我一直认为你应该只根据需要声明变量,
并隐含地似乎很多作者鼓励它,但另一个
一天朋友告诉我,在循环中声明变量并不是很好的练习,例如:

for(int i = 0; i< size-1; ++ i ){
int aux = array [i];
array [i] = array [i + 1];
array [i + 1] = aux;
}

应该写成:

int aux;
for(int i = 0; i< size-1; ++ i){
aux = array [i];
array [i] = array [i + 1];
array [i + 1] = aux;
}

你是做什么的认为? Thaks!
I have always felt that you should only declared variables as needed,
and implicitily it seems many authors to encourage it, but the other
day a friend told me that declaring variables inside a loop wasn''t good
practice, something like:

for(int i=0; i<size-1; ++i){
int aux = array[i];
array[i]=array[i+1];
array[i+1]=aux;
}

should be written like:

int aux;
for(int i=0; i<size-1; ++i){
aux = array[i];
array[i]=array[i+1];
array[i+1]=aux;
}

What do you think? Thaks!



因为'aux''只在循环中有意义,为什么不将它的范围限制为

到循环内的块?


另一方面,有些情况下你不想这样做,例如,当有问题的变量属于
需要大量的建设费用。然而,这似乎不是这些情况之一。 ;-)


HTH,

- g


-

Artie Gold - 德克萨斯州奥斯汀
http://goldsays.blogspot.com

你不能亲吻*除非你错过**

[* - 保持简单,愚蠢。 ** - 简单,愚蠢。]


Since `aux'' is only meaningful inside the loop why not limit its scope
to the block inside the loop?

On the other hand, there are situations where you wouldn''t want to do
this, when, for example, the variable in question is of a type that
requires significant construction overhead. This, however, seems not to
be one of those situations. ;-)

HTH,
--ag

--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com
"You can''t KISS* unless you MISS**"
[*-Keep it simple, stupid. **-Make it simple, stupid.]


Gaijinco写道:
Gaijinco wrote:
我一直认为你应该只声明变量根据需要,
并隐含地似乎很多作者都鼓励它,但另一天,一位朋友告诉我,在循环中声明变量并不是很好的练习,例如:

for(int i = 0; i< size-1; ++ i){
int aux = array [i];
array [i] = array [i + 1];
数组[i + 1] = aux;
}
应该写成:

int aux;
for (int i = 0; i< size-1; ++ i){
aux = array [i];
array [i] = array [i + 1];
array [ i + 1] = aux;
}

你怎么看? Thaks!
I have always felt that you should only declared variables as needed,
and implicitily it seems many authors to encourage it, but the other
day a friend told me that declaring variables inside a loop wasn''t good
practice, something like:

for(int i=0; i<size-1; ++i){
int aux = array[i];
array[i]=array[i+1];
array[i+1]=aux;
}

should be written like:

int aux;
for(int i=0; i<size-1; ++i){
aux = array[i];
array[i]=array[i+1];
array[i+1]=aux;
}

What do you think? Thaks!




您的朋友可能会认为第一个代码速度较慢。在一些类似的

案例中,它可能确实会更慢。你不应该担心这个。


告诉你的朋友,过早的优化是万恶之源。

最重要的优化资源是程序员时间。程序员必须先用
编写干净的代码,因为它很容易制作漂亮的代码而不是快速代码很好的代码。


for循环结束后,aux'的唯一含义是

数组中的最后一个变量。后续陈述不应依赖于for循环泄漏。它的

最后一个辅助值。如果他们想要数组中的最后一个值,他们应该自己获得它。他们应该尽可能地与你的程序的其余部分分开,包括他们之前的陈述。


总是给任何标识符最小的范围和访问权限可能。不要
使用全局变量或公共数据成员。永远不要创建一个变量

而不给它一个初始值。把所有这些规则放在一起,你就好了b $ b有很多理由将int aux放在循环中。


-

Phlip
http://www.greencheese.org/ZeekLand < - - 不是博客!!!



Your friend might think that the first code is slower. In some few similar
cases it might indeed be slower. You shouldn''t worry about that.

Tell your friend that premature optimization is the root of all evil. The
most important resource to optimize is programmer time. Programmers must
write clean code first, because it''s easy to make beautiful code fast than
fast code beautiful.

After your for loop ends, aux''s only meaning is "the last variable in the
array". Subsequent statements should not rely on the for loop "leaking" its
last aux value out. If they want the last value in the array, they should
get it themselves. They should decouple as much as possible from the rest
of your program, including the statements just before them.

Always give any identifier the narrowest scope and access possible. Don''t
use global variables or public data members. Never create a variable
without giving it an initial value. Put all these rules together, and you
have many reasons to put int aux inside the loop.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!


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

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