只有一点回报 [英] Only one point of return

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

问题描述

一位同事告诉我,有一个关于良好类型的规则,C ++中的一个

函数应该只有一个返回点(即返回

语句)。否则可能会有麻烦。

我从来没有听说过它并对此表示怀疑。

是否有人听说过它?有什么好处?


问候,

Marc


例如:

bool f()

{

如果(!pointer1)返回false;

pointer1-> doSomething();


if(!pointer2)返回false;

pointer2-> doSomething1();


返回true ;

}


vs.


bool f()

{

bool retVal = true;

if(pointer1)

{

pointer1-> doSomething(); < br $>
}

其他

retVal = false;


if(pointer2)

{

pointer2-> doSomething();

}

else

retVal = false;


返回retVal;

}

A colleague told me that there is a rule about good stype that a
function in C++ should have only one point of return (ie. return
statement). Otherwise there might be trouble.
I never heard about it and doubt it.
Anybody heard of it? What would be the advantage?

Regards,
Marc

Example:

bool f()
{
if( !pointer1) return false;
pointer1->doSomething();

if( !pointer2) return false;
pointer2->doSomething1();

return true;
}

vs.

bool f()
{
bool retVal=true;
if( pointer1)
{
pointer1->doSomething();
}
else
retVal=false;

if( pointer2)
{
pointer2->doSomething();
}
else
retVal=false;

return retVal;
}

推荐答案

< a href =mailto:cp ******** @ googlemail.com> cp ******** @ googlemail.com 写道:

一位同事告诉我,有一条关于善的规则在C ++中使用

函数应该只有一个返回点(即。返回

声明)。否则可能会有麻烦。

我从来没有听说过它并对此表示怀疑。

是否有人听说过它?有什么好处?
A colleague told me that there is a rule about good stype that a
function in C++ should have only one point of return (ie. return
statement). Otherwise there might be trouble.
I never heard about it and doubt it.
Anybody heard of it? What would be the advantage?



这是一个相当常见的编码标准。


在我看来,它更有意义C比C ++,只要你的代码是

异常安全,早期退货不应该造成任何伤害。有些人可能认为

单点返回会使调试变得更容易。


C或C ++早期返回的常见问题并非例外

安全是资源泄漏,有问题的资源只在功能结束时或最后一次返回之前发布




-

Ian Collins。

It is a fairly common coding standard.

In my opinion it makes more sense for C than C++, provided your code is
exception safe an early return shouldn''t do any harm. Some may argue
that a single point of return makes debugging easier.

A common problem early returns with either C or C++ that isn''t exception
safe is resource leaks where the resource in question is only released
at the end of the function or before the last return.

--
Ian Collins.




< cp ******** @ googlemail .comwrote in message

news:11 ********************** @ o61g2000hsh.googlegr oups.com ...

<cp********@googlemail.comwrote in message
news:11**********************@o61g2000hsh.googlegr oups.com...

>一位同事告诉我,有一个关于良好类型的规则,C ++中的

函数应该只有一个返回点(即。返回

声明)。否则可能会有麻烦。

我从来没有听说过它并对此表示怀疑。

是否有人听说过它?有什么好处?


问候,

Marc


例如:

bool f()

{

如果(!pointer1)返回false;

pointer1-> doSomething();


if(!pointer2)返回false;

pointer2-> doSomething1();


返回true ;

}


vs.


bool f()

{

bool retVal = true;

if(pointer1)

{

pointer1-> doSomething(); < br $>
}

其他

retVal = false;


if(pointer2)

{

pointer2-> doSomething();

}

else

retVal = false;


返回retVal;

}
>A colleague told me that there is a rule about good stype that a
function in C++ should have only one point of return (ie. return
statement). Otherwise there might be trouble.
I never heard about it and doubt it.
Anybody heard of it? What would be the advantage?

Regards,
Marc

Example:

bool f()
{
if( !pointer1) return false;
pointer1->doSomething();

if( !pointer2) return false;
pointer2->doSomething1();

return true;
}

vs.

bool f()
{
bool retVal=true;
if( pointer1)
{
pointer1->doSomething();
}
else
retVal=false;

if( pointer2)
{
pointer2->doSomething();
}
else
retVal=false;

return retVal;
}



第二个例子不同于第一个例子

它会转到指针2部分,即使指针1不存在


每个人都有一个意见,但是在C ++中有IMO,维持

a单身入口点导致更多混淆代码

而不是更少(这是,我相信,

支持它的论点。)

This second example is different than the first since
it goes to the pointer2 part even if pointer1 doesn''t
exist.
Everyone has an opinion but IMO in C++, maintaining
a single entry point leads to more obfuscated code
rather than less (which is, I believe, the argument in
favor of it.)




" duane hebert" < sp ** @flarn.comwrote in message

news:9p ******************** @ weber.videotron.net .. 。

"duane hebert" <sp**@flarn.comwrote in message
news:9p********************@weber.videotron.net...

第二个例子与第一个例子不同,因为

即使指针1不是

存在。

每个人都有意见,但IMO在C ++中,保持单个入口点
a导致更多混淆代码
This second example is different than the first since
it goes to the pointer2 part even if pointer1 doesn''t
exist.
Everyone has an opinion but IMO in C++, maintaining
a single entry point leads to more obfuscated code



a单出口点(duh)

a single exit point (duh)


这篇关于只有一点回报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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