【发布时间】:2013-06-29 07:26:37
【问题描述】:
我有相当多的编程经验,但是多年来不断编写函数,我只是想知道社区对这件事的看法。
在函数下面是声明所有变量的最佳位置是在最开始还是在你去的时候声明它们?
例如:
void fake_function1() {
int i;
//do something here with variable i
int counter;
//do something here with variable counter
}
or
void fake_function2() {
int i;
int counter;
//do something here with variable i
//do something here with variable counter
}
到目前为止,我通常倾向于做类似 fake_function2() 的事情,因为这看起来更正确,但有时我会做类似 fake_function1() 的事情,因为它看起来更具可读性和可读性代码总是更好的代码,尤其是当在我看来,代码可以轻松运行超过 10 万行。我认为一致性非常重要,但我很难决定哪个更好。
【问题讨论】: