C++ 使代码更干净
对不起,如果这个问题不适合这样做C++ 使代码更干净,c++,C++,对不起,如果这个问题不适合这样做 我有一个C++函数,它看起来像MyFun.(),下面给出。 从这个函数中,我调用了一些(大约30个)其他函数,它们返回一个布尔变量(true表示成功,false表示失败)。如果这些函数中的任何一个返回false,我也必须从MyFun()返回false。此外,如果中间函数调用失败,我不应该立即退出(不调用其余函数) 目前我正在做下面给出的事情,但我觉得可以有一个更简洁的方法来处理这个问题。如有任何建议,我们将不胜感激 非常感谢 bool MyFun() // fn
我有一个C++函数,它看起来像MyFun.(),下面给出。 从这个函数中,我调用了一些(大约30个)其他函数,它们返回一个布尔变量(true表示成功,false表示失败)。如果这些函数中的任何一个返回false,我也必须从MyFun()返回false。此外,如果中间函数调用失败,我不应该立即退出(不调用其余函数)
目前我正在做下面给出的事情,但我觉得可以有一个更简洁的方法来处理这个问题。如有任何建议,我们将不胜感激 非常感谢bool MyFun() // fn that returns false on failure
{
bool Result = true;
if (false == AnotherFn1()) // Another fn that returns false on failure
{
Result = false;
}
if (false == AnotherFn2()) // Another fn that returns false on failure
{
Result = false;
}
// Repeat this a number of times.
.
.
.
if (false == Result)
{
cout << "Some function call failed";
}
return Result;
}
bool MyFun()//失败时返回false的fn
{
布尔结果=真;
if(false==AnotherFn1())//另一个失败时返回false的fn
{
结果=假;
}
if(false==AnotherFn2())//另一个失败时返回false的fn
{
结果=假;
}
//重复此操作数次。
.
.
.
if(false==结果)
{
cout!可用作假的清洁替代品
像这样:
bool MyFun() // fn that returns false on failure
{
bool Result = true;
if (!AnotherFn1()) // Another fn that returns false on failure
{
Result = false;
}
if (!AnotherFn2()) // Another fn that returns false on failure
{
Result = false;
}
// Repeat this a number of times.
.
.
.
if (!Result)
{
cout << "Some function call failed";
}
return Result;
bool MyFun()//失败时返回false的fn
{
布尔结果=真;
if(!AnotherFn1())//另一个失败时返回false的fn
{
结果=假;
}
if(!AnotherFn2())//另一个失败时返回false的fn
{
结果=假;
}
//重复此操作数次。
.
.
.
如果(!结果)
{
coutI将用更精确的按位AND赋值替换每个if
语句:
bool MyFun() // fn that returns false on failure
{
bool Result = true;
Result &= AnotherFn1(); // Another fn that returns false on failure
Result &= AnotherFn2(); // Another fn that returns false on failure
// Repeat this a number of times.
.
.
.
if (false == Result)
{
cout << "Some function call failed";
}
return Result;
}
bool MyFun()//失败时返回false的fn
{
布尔结果=真;
结果&=AnotherFn1();//另一个失败时返回false的fn
Result&=AnotherFn2();//另一个失败时返回false的fn
//重复此操作数次。
.
.
.
if(false==结果)
{
cout如何使用异常来处理故障:
主要问题是,函数调用是否相互依赖?如果前一个函数调用失败,是否可以跳过某些函数调用?…而不是
if (false == AnotherFn1()) // Another fn that returns false on failure
{
Result = false;
}
if (false == AnotherFn2()) // Another fn that returns false on failure
{
Result = false;
}
if (false == AnotherFn3()) // Another fn that returns false on failure
{
Result = false;
}
开始使用布尔值,真值:
那么,所有这些条件都有相同的代码;它们基本上是一个大条件的一部分:
if ( !AnotherFn1()
| !AnotherFn2()
| !AnotherFn3())
{
Result = false;
}
对于您希望调用所有函数的特定问题,即使您很早就知道将返回false
,但重要的是不要使用短路运算符&
和|
。使用热切的位运算符
和&
确实是一种技巧,因为它们是位运算符,而不是布尔运算符(从而隐藏意图),但在您的情况下工作iffAnotherFn?
return strictbool
s
您可以否定您在内部执行的操作;否定越少,生成的代码越好:
Result = false;
if ( AnotherFn1()
& AnotherFn2()
& AnotherFn3())
{
Result = true;
}
然后你可以去掉这些任务,直接返回:
if ( AnotherFn1()
& AnotherFn2()
& AnotherFn3())
{
return true;
}
cout << "something bad happened";
return false;
if(另一个fn1()
&另一个fn2()
&另一个fn3()
{
返回true;
}
不能使用std::function
的std::vector
之类的东西。它更易于维护
例如:
我正在寻找类似的东西。这将有助于减少代码行。谢谢。@HAL9000-更好地使用&&=而不是&=。请更新answer@egur:我完全同意你的看法!我将编辑我的答案,谢谢。@egur:通过看这里,我改变了主意,留下了我的原始答案。我认为这个答案很完美,并且使用了按位&使更多的人能够理解se,特别是如果OP更喜欢将所有&=语句合并为类似Result&=AnotherFn1()&AnotherFn2()和AnotherFn3()的内容
这是不同的行为。在旧版本中,所有函数都会被计算,而不是在您的版本中。@phresnel我想您是这样的right@Johan:我忽略了这是一个要求。我更改了答案。非常好的方式。我不知道这是可能的。这样我就可以放弃我丑陋的函数指针。这是我会推荐的。我如果选择了一个带有FordDebug的C++解决方案,那就很有趣了。我不知道是否可以使用累积。@ RoBrtButter类似于 STD::Stand(函数。
可以使用,但这并不能使代码更清晰。如果这个问题适合这样的话,很抱歉
天啊,我知道我讨厌这样做happens@LightnessRacesinOrbit:打字错误……。:)这个问题似乎离题了,因为它属于@stefan:你真的提出了这个问题吗?对不起,我对这个问题不太熟悉。@NeonGlow:公平地说,这是真的。
if ( AnotherFn1()
& AnotherFn2()
& AnotherFn3())
{
return true;
}
cout << "something bad happened";
return false;
bool MyFun() // fn that returns false on failure
{
bool Result = true;
if (false == AnotherFn1()) // Another fn that returns false on failure
{
Result = false;
}
if (false == AnotherFn2()) // Another fn that returns false on failure
{
Result = false;
}
// Repeat this a number of times.
.
.
.
if (false == Result)
{
cout << "Some function call failed";
}
return Result;
}
bool MyFun() // fn that returns false on failure
{
if (AnotherFn1() &
AnotherFn2() &
AnotherFn3())
{
return true;
}
cout << "Some function call failed";
return false;
}
bool MyFun() // fn that returns false on failure
{
bool Result = true;
// if need to call every function, despite of the Result of the previous
Result = AnotherFn1() && Result;
Result = AnotherFn2() && Result;
// if need to avoid calling any other function after some failure
Result = Result && AnotherFn1();
Result = Result && AnotherFn2();
return Result;
}
// List all the function you want to evaluate
std::vector<std::function<bool()>> functions = {
my_func1,
my_func2,
my_func3,
my_func4
};
// Evaluate all the function returning the number of function that did fail.
unsigned long failure =
std::count_if(functions.begin(), functions.end(),
[](const std::function<bool()>& function) { return !function(); });
std::map<std::string, std::function<bool()>> function_map;