使代码更清洁

时间:2013-12-10 12:06:55

标签: c++

很抱歉,如果这个问题不适合SO。

我有一个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;
}

6 个答案:

答案 0 :(得分:14)

我会用一个更加符合的按位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;
}

答案 1 :(得分:11)

使用std::vector std::function之类的内容。它更加可维护。

示例:http://ideone.com/0voxRl

// 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::all_of代替std::count_if。您将控制流与功能列表分离,这在我看来是一件好事。

您可以使用名称为键的函数映射来改进此功能,以便输出哪个函数失败:

std::map<std::string, std::function<bool()>> function_map;

答案 2 :(得分:5)

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;
}

答案 3 :(得分:3)

而不是

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()) // Another fn that returns false on failure
{
    Result = false;
}

if (!AnotherFn2()) // Another fn that returns false on failure
{
    Result = false;
}

if (!AnotherFn3()) // Another fn that returns false on failure
{
    Result = false;
}

然后,所有这些条件具有相同的代码;它们基本上是一个重大条件的一部分:

if ( !AnotherFn1()
   | !AnotherFn2()
   | !AnotherFn3())
{
    Result = false;
}

针对您的特定问题,您希望调用所有函数,即使您早知道将返回false,也不要使用短路运算符&&和{{1 }}。使用急切的按位运算符|||实际上是一个hack,因为它们是按位而不是布尔值(从而隐藏意图),但是在你的情况下工作iff & return strict {{ 1}} S上。

你可以否定你在里面做的事;减少否定产生更好的代码:

AnotherFn?

然后你可以摆脱这些任务,而是直接回归:

bool

摘要

旧:

Result = false;


if ( AnotherFn1()
   & AnotherFn2()
   & AnotherFn3())
{
    Result = true;
}

新:

if ( AnotherFn1()
   & AnotherFn2()
   & AnotherFn3())
{
    return true;
}

cout << "something bad happened";
return false;

还有更多可能的改进,例如:使用异常代替错误代码,但不要试图处理“预期”。

答案 4 :(得分:0)

可用作 false

的清洁替代品

像这样:

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;

}

答案 5 :(得分:0)

如何使用异常处理失败:a neat exemple

主要问题是函数调用是否相互依赖?如果前一个失败,可以跳过一些吗? ...