代码片段的“可比性”(来自“代码完整”一书) [英] `Comparability' of code snippets (from book `Code complete')

查看:86
本文介绍了代码片段的“可比性”(来自“代码完整”一书)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

完成代码一书提到了类似的代码片段,分别是

,并分别讨论了它们的优点。


但我认为这些代码片段在逻辑上是完全不同的,并且它们的功能是不同的。他们没有可比性。

我是对的吗?

/ *代码1 * /

for(i = 0; i< ; N; i ++){

if(cond)

DoSomething();

else

DoOtherthing() ;

}


/ * code 2 * /

if(cond){

for (i = 0; i< N; i ++)

DoSomething();

} else {

for(i = 0; i < N; i ++)

DoOtherthing();

}

The book `Code complete'' mentions similar code snippets as the
followings , and talks about their advantages respectively.

But I think these code snippets are totally different in logic, and
their functionalities are different. They don''t have comparabilities.
Am I right?
/*code 1*/
for (i = 0; i < N; i++){
if (cond)
DoSomething();
else
DoOtherthing();
}

/*code 2*/
if (cond){
for (i = 0; i < N; i++)
DoSomething();
} else {
for (i = 0; i < N; i++)
DoOtherthing();
}

推荐答案

lovecreatesbea。 .. @ gmail.com说:
lovecreatesbea...@gmail.com said:

完成代码一书提到类似的代码片段如下

,并谈及分别是他们的优势。


但我认为这些代码片段的逻辑完全不同,而且他们的功能是不同的。他们没有可比性。

我是对的吗?
The book `Code complete'' mentions similar code snippets as the
followings , and talks about their advantages respectively.

But I think these code snippets are totally different in logic, and
their functionalities are different. They don''t have comparabilities.
Am I right?



这取决于未显示的细节。


-

Richard Heathfield< ; http://www.cpax.org.uk>

电子邮件:-http:// www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日

It depends on details that are not shown.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999




" lovecreatesbea ... @ gmail.com" < lo *************** @ gmail.comwrote in message

news:d0 *************** ******************* @ s36g2000 prg.googlegroups.com ...

"lovecreatesbea...@gmail.com" <lo***************@gmail.comwrote in message
news:d0**********************************@s36g2000 prg.googlegroups.com...

书籍`代码完整''提到了类似的代码片段,分别是关于他们的优点。


但我认为这些代码片段的逻辑完全不同,

他们的功能是不同的。他们没有可比性。

我是对的吗?


/ *代码1 * /

for(i = 0; i< N; i ++){

if(cond)

DoSomething();

else

DoOtherthing();

}


/ * code 2 * /

if(cond){

for(i = 0; i< N; i ++)

DoSomething();

} else {

for(i = 0; i< N; i ++)

DoOtherthing();

}
The book `Code complete'' mentions similar code snippets as the
followings , and talks about their advantages respectively.

But I think these code snippets are totally different in logic, and
their functionalities are different. They don''t have comparabilities.
Am I right?
/*code 1*/
for (i = 0; i < N; i++){
if (cond)
DoSomething();
else
DoOtherthing();
}

/*code 2*/
if (cond){
for (i = 0; i < N; i++)
DoSomething();
} else {
for (i = 0; i < N; i++)
DoOtherthing();
}



假设没有尝试要改变cond,i或N中的函数,它们应该具有完全相同的效果。


问题是哪个更有效,哪个是更好的风格。


-

免费游戏和编程好东西。
http://www.personal.leeds.ac.uk/~bgy1mm

Assuming no attempt to alter cond, i or N in the functions, they should have
exactly the same effect.

The question is which is more efficient, and which is better style.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm


lovecreatesbea ... @ gmail.com写道:
lovecreatesbea...@gmail.com wrote:

完整代码一书提到了类似的代码片段,分别是

,并分别讨论了它们的优点。


但是我认为这些代码片段在逻辑上是完全不同的,并且它们的功能是不同的。他们没有可比性。

我是对的吗?


/ *代码1 * /

for(i = 0; i< N; i ++){

if(cond)

DoSomething();

else

DoOtherthing();

}


/ * code 2 * /

if(cond){

for(i = 0; i< N; i ++)

DoSomething();

} else {

for(i = 0; i< N; i ++)

DoOtherthing();

}
The book `Code complete'' mentions similar code snippets as the
followings , and talks about their advantages respectively.

But I think these code snippets are totally different in logic, and
their functionalities are different. They don''t have comparabilities.
Am I right?
/*code 1*/
for (i = 0; i < N; i++){
if (cond)
DoSomething();
else
DoOtherthing();
}

/*code 2*/
if (cond){
for (i = 0; i < N; i++)
DoSomething();
} else {
for (i = 0; i < N; i++)
DoOtherthing();
}



既不提供DoSomething ()也没有DoNothing()改变cond

或i的值,它们是相同的,一个好的优化编译器应该能够生成相同的机器码。

Provided neither DoSomething() nor DoNothing() alter the value of cond
or i, they''re identical, and a good optimising compiler should be able
to generate the same machinecode.


这篇关于代码片段的“可比性”(来自“代码完整”一书)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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