代码风格 - 处理退货 [英] Code Style - Handling Returns

查看:64
本文介绍了代码风格 - 处理退货的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在编写一些代码时,我意识到我从未开发出一致的

模式来检查方法中的错误。我有两种风格我之间来回跳转,但我想知道哪种更好。


情景:

*返回成功/失败的方法

*也返回成功/失败的调用方法

*需要检查返回状态并继续或退出外部方法


我个人认为Case 1看起来有点干净但是比案例2打破了更多

。任何意见?

案例1:


bool flag = myFunc1();

if(flag == false)

{

return;

}


flag = myFunc2();

if(flag == false)

{

返回;

}


//更多代码

案例2:


bool flag = myFunc1();

if(flag == true)

{

flag = myFunc2();

if(flag == true)

{

flag = myFunc3();

if(flag == true)

{

//继续代码

}

}

}

While writing some code, I realized I had never developed a consistent
pattern for checking errors from a method. I have two styles I jump
back and forth between, but I''m wondering which is better.

Scenario:
* In method that returns success/fail
* Call method that also returns success/fail
* Need to check return state and continue or exit outer method

I personally think Case 1 looks a little cleaner but it breaks more
than case 2. Any opinions?
Case 1:

bool flag = myFunc1( );
if( flag == false )
{
return;
}

flag = myFunc2( );
if( flag == false )
{
return;
}

// more code
Case 2:

bool flag = myFunc1( );
if( flag == true )
{
flag = myFunc2( );
if( flag == true )
{
flag = myFunc3( );
if( flag == true )
{
// continue code
}
}
}

推荐答案

Jordan写道:
在编写代码时,我意识到我从来没有开发出一致的
模式来检查方法中的错误。我有两种风格,我之间来回跳转,但我想知道哪种更好。

场景:
*在返回成功/失败的方法中 *也会返回成功/失败的调用方法
*需要检查返回状态并继续或退出外部方法

我个人认为案例1看起来更清洁但是它打破了更多
案例2.任何意见?
While writing some code, I realized I had never developed a consistent
pattern for checking errors from a method. I have two styles I jump
back and forth between, but I''m wondering which is better.

Scenario:
* In method that returns success/fail
* Call method that also returns success/fail
* Need to check return state and continue or exit outer method

I personally think Case 1 looks a little cleaner but it breaks more
than case 2. Any opinions?




我更倾向于使用RAII类的案例1来处理清理,这使得

它都是异常安全的并且更不容易出错代码

维护添加一个新的if / return块。



I tend to prefer case 1 with RAII classes to handle cleanup which makes
it both exception safe and much less prone to error from code
maintenance adding a new if/return block.


Jordan写道:
在编写代码时,我意识到我从来没有开发出一致的
模式来检查方法中的错误。我有两种风格我来回跳动,但我想知道哪种更好。


无论您的个人喜好如何,写下


if(flag == false)


很傻。 ''flag''已经有类型''bool'',所以只需写一下


if(!flag)


同样适用于


if(flag == true)


..只需写下


if(flag)


并删除== true杂乱。

场景:
*在返回成功/失败的方法中
*也返回成功/失败的调用方法
*需要检查返回状态并继续或退出外部方法

我个人认为Case 1看起来有点干净,但它比案例2打破了更多
任何意见?

案例1:

bool flag = myFunc1();
if(flag == false)
{
返回;
}

flag = myFunc2( );
if(flag == false)
{
返回;
}

//更多代码

案例2:

bool flag = myFunc1();
if(flag == true)
{
flag = myFunc2();
if(flag == true)
{
flag = myFunc3();
if(flag == true)
{
//继续代码
}
}
}
While writing some code, I realized I had never developed a consistent
pattern for checking errors from a method. I have two styles I jump
back and forth between, but I''m wondering which is better.
Regardless of your personal preference, writing

if (flag == false)

is silly. ''flag'' already has type ''bool'', so just write

if (!flag)

Same goes for

if (flag == true)

.. Just write

if (flag)

and drop the "== true" clutter.

Scenario:
* In method that returns success/fail
* Call method that also returns success/fail
* Need to check return state and continue or exit outer method

I personally think Case 1 looks a little cleaner but it breaks more
than case 2. Any opinions?
Case 1:

bool flag = myFunc1( );
if( flag == false )
{
return;
}

flag = myFunc2( );
if( flag == false )
{
return;
}

// more code
Case 2:

bool flag = myFunc1( );
if( flag == true )
{
flag = myFunc2( );
if( flag == true )
{
flag = myFunc3( );
if( flag == true )
{
// continue code
}
}
}




两者都不比另一个好。


我更喜欢


if(!myFunc1()||!myFunc2())

返回;

//继续代码


V



Neither is better than the other.

I prefer

if (!myFunc1() || !myFunc2())
return;
// continue code

V


[snip]
很傻。 ''flag''已经有类型''bool'',所以只需写一下

if(!flag)
is silly. ''flag'' already has type ''bool'', so just write

if (!flag)




我个人更喜欢

如果(不是标志)

当我正在阅读时,我总是有一些难以看到_!_运算符

代码(特别是当我是匆忙,几乎总是如此。


问候,


Marcelo Pinto



I personally prefer
if(not flag)
I always have some dificulty to see the _!_ operator when I am reading
code (specially when I am in a rush, which is almost always).

Regards,

Marcelo Pinto


这篇关于代码风格 - 处理退货的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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