长函数调用的可读性

Ple*_*rts 2 c++ readability

我一直在编写使用函数调用的代码,这些函数调用非常长并且通常超过80个字符.通常我会像这样拆分这些函数调用:

LongFunctionName(first_argument,
                 another_argument,
                 finally_last_argument);
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试将它放入if语句时,它看起来很奇怪,主要是因为它变得不太清楚它与之比较的值是什么:

if(LongFunctionName(first_argument,
                    another_argument,
                    finally_last_argument) != value_compared_to)
{
    // stuff to be called
}
Run Code Online (Sandbox Code Playgroud)

您如何将此语句格式化为更易读并且适合80个字符?

Jos*_*eld 8

我会考虑将函数调用放在自己的行上:

const auto value = LongFunctionName(first_argument,
                              another_argument,
                              finally_last_argument);
if (value != value_compared_to) {
  // ...
}
Run Code Online (Sandbox Code Playgroud)

您甚至可以为value变量提供一个很好的描述性名称,以帮助理解代码.

  • 该区域仍然需要存在于内存中,即使它没有名称. (2认同)