应该在函数体中改变函数参数吗? [英] Should function argument be changed in function body?

查看:76
本文介绍了应该在函数体中改变函数参数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我被教导说,参数valuse不应该在

函数体中改变。比如,下面的代码不好。

void foo1(int x)

{

x ++;

printf(" x + 1 =%d \ n",x);

}

它应该是重构的要是

void foo2(int x)

{

int y = x;

y ++;

printf(" x + 1 =%d \ n",y);

}


最近,我发现一个人在一个C lang论坛上发布了像foo1这样的代码。我b
$ b指出它不是很好的代码标准。令人惊讶的是,似乎所有人都批评我作为教条主义。据称,更改arugment

值不会影响调用者代码,并且可以节省堆栈大小。我理解

他们的观点,但是我记得通常认为参数不是

我认为要改变。


现在我很迷惑。是否有人可以解释为什么它是什么?b $ b b代码规则不应该更改参数?

谢谢&问候

Hi All,

I was taught that argument valuse is not supposed to be changed in
function body. Say, below code is not good.
void foo1(int x)
{
x ++;
printf("x+1 = %d\n", x);
}
It should be "refactor-ed" to be
void foo2(int x)
{
int y = x;
y ++;
printf("x + 1 = %d\n", y);
}

Recently, I found one guy posted code like foo1 in one C lang forum. I
pointed out that it is not good code standard. Surprisingly, seems all
people criticise me as dogmatism. It is claimed that changing arugment
value will not affect caller code and it saves stack size. I understand
their point, but I DO remember it is commmon that argument is not
suppsoed to be changed.

Now I am confused. Is there anybody can give some explaination why it is
a code rule that argument should not be changed?
Thanks & Regards

推荐答案

Morgan Cheng写道:
Morgan Cheng wrote:

.... snip ...
现在我很困惑。是否有人可以给出一些解释
为什么不应该更改参数的代码规则?
.... snip ...
Now I am confused. Is there anybody can give some explaination
why it is a code rule that argument should not be changed?




不修改参数的唯一原因是初始值
以后需要
。任何这样的规则都是纯粹的愚蠢。你应该把b / b $ b $作为外部初始化的局部变量(

就是它们)。例如:


int putblanks(size_t n,FILE * f)

{

while(n - )

if(EOF == putc('''',f))返回EOF;

返回'''';

}


-

"如果你想通过groups.google.com发布后续内容,请不要使用

破碎"回复"链接在文章的底部。点击

" show options"在文章的顶部,然后点击

回复在文章标题的底部。 - Keith Thompson



The only reason not to modify arguments is that the initial value
is required later. Any such rule is pure foolishness. You should
think of arguments as externally initialized local variables (which
is precisely what they are). For example:

int putblanks(size_t n, FILE *f)
{
while (n--)
if (EOF == putc('' '', f)) return EOF;
return '' '';
}

--
"If you want to post a followup via groups.google.com, don''t use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson





Morgan Cheng写道:


Morgan Cheng wrote:
大家好,
<我被告知,在函数体中不应该改变参数valuse。说,下面的代码不好。
void foo1(int x)
{
x ++;
printf(" x + 1 =%d \ n", x);
}
它应该是重构的。
void foo2(int x)
{
int y = x;
y ++;
printf(" x + 1 =%d \ n,y);
}
最近,我发现一个人在一个C lang论坛上发布了像foo1这样的代码。我指出它不是很好的代码标准。令人惊讶的是,似乎所有人都批评我作为教条主义。声称改变调整值不会影响调用者代码,并且可以节省堆栈大小。我理解他们的观点,但是我记得通常认为参数不会被改变。

现在我很困惑。是否有人可以解释为什么它是一个不应该改变论证的代码规则?

谢谢&问候
Hi All,

I was taught that argument valuse is not supposed to be changed in
function body. Say, below code is not good.
void foo1(int x)
{
x ++;
printf("x+1 = %d\n", x);
}
It should be "refactor-ed" to be
void foo2(int x)
{
int y = x;
y ++;
printf("x + 1 = %d\n", y);
}

Recently, I found one guy posted code like foo1 in one C lang forum. I
pointed out that it is not good code standard. Surprisingly, seems all
people criticise me as dogmatism. It is claimed that changing arugment
value will not affect caller code and it saves stack size. I understand
their point, but I DO remember it is commmon that argument is not
suppsoed to be changed.

Now I am confused. Is there anybody can give some explaination why it is
a code rule that argument should not be changed?
Thanks & Regards




是的,改变参数值真的不是一个好习惯。我想尝试使用asm来解释它,尽管我只是BEGIN学习它。

使用gcc将上面的代码更改为asm,并查找

的差异。你的b $ b将找到函数参数是推入''堆栈'并返回

值是在%eax寄存器中。如果你更改4(%esp)中的参数,它

是坏的当然。我也是如此。

如果我不对,请立即纠正!我只是试一试! : - (



Yeah,changing argument values is really not a good habit.I wanna try to
explain it using asm,though I just BEGIN to learn it.
Use gcc to change the code above into asm, and look for the
differences.You
will find function arguments are pushed into ''stack'' and the return
value is in %eax register.If you change the argument in the 4(%esp),it
is bad of course.Er,just so so.
If I am not right,correct this at once! I just have a try! :-(


Morgan Cheng写道:
Morgan Cheng wrote:
大家好,

我被教导了这个论点在函数体中不应该改变valuse。比如说,下面的代码不好。
void foo1(int x)
{
x ++;
printf(x + 1 =%d \ n,x);
}
它应该被重构为
void foo2(int x)
{
int y = x;
y ++;
printf(" x + 1 =%d \ n",y);
}
最近,我发现一个人在一个C lang论坛上发布了像foo1这样的代码。我指出它不是很好的代码标准。令人惊讶的是,似乎所有人都批评我作为教条主义。声称改变调整值不会影响调用者代码并且它可以节省堆栈大小。我理解他们的观点,但我记得通常认为参数不被改变。 。

现在我很困惑。有没有人可以给一些前任为什么它是一个不应该改变参数的代码规则?
Hi All,

I was taught that argument valuse is not supposed to be changed in
function body. Say, below code is not good.
void foo1(int x)
{
x ++;
printf("x+1 = %d\n", x);
}
It should be "refactor-ed" to be
void foo2(int x)
{
int y = x;
y ++;
printf("x + 1 = %d\n", y);
}

Recently, I found one guy posted code like foo1 in one C lang forum. I
pointed out that it is not good code standard. Surprisingly, seems all
people criticise me as dogmatism. It is claimed that changing arugment
value will not affect caller code and it saves stack size. I understand
their point, but I DO remember it is commmon that argument is not
suppsoed to be changed.

Now I am confused. Is there anybody can give some explaination why it is
a code rule that argument should not be changed?




正如Chuck Falconer指出的那样,唯一的实用理由_not_

改变一个参数值是你以后需要的。

一个_could_可能也认为论证不应该被滥用

用于其他目的,例如使用参数green_val来存储完全不相关的计算的结果(而不是使用apt名称引入

a临时变量,比如说wobble_factor)。

这些是我现在能想出来的唯一原因。

制定你的规则成为编码标准的强制性规则确实是不必要的教条主义接近纯粹的愚蠢。

干杯

Michael

-

电子邮件:我的是/ at / gmx / dot / de地址。



As Chuck Falconer pointed out, the only pragmatical reason _not_ to
change an argument value is that you need it later on.
One _could_ probably argue also that an argument should not be "abused"
for another purpose, e.g. using an argument green_val to store the
result of a completely unrelated computation (instead of introducing
a temporary variable with an apt name, say wobble_factor).
These are the only reasons I can come up with right now.

Making your "rule" into a mandatory rule for a coding standard is indeed
unnecessary dogmatism bordering on sheer folly.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.


这篇关于应该在函数体中改变函数参数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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