长函数调用的可读性
Readability of long function calls
我一直在编写使用函数调用的代码,这些函数调用非常长,通常超过80个字符。通常我会像这样拆分这些函数调用:
LongFunctionName(first_argument,
another_argument,
finally_last_argument);
然而,当我试图将它放入if语句中时,它看起来很奇怪,主要是因为它变得不太清楚它比较的是什么值:
if(LongFunctionName(first_argument,
another_argument,
finally_last_argument) != value_compared_to)
{
// stuff to be called
}
你会如何格式化这个语句,使其更易于阅读,并适合80个字符?
我会考虑将函数调用放在单独的行中:
const auto value = LongFunctionName(first_argument,
another_argument,
finally_last_argument);
if (value != value_compared_to) {
// ...
}
您甚至可以给value
变量一个很好的描述性名称,以帮助理解代码。
将返回值存储在变量中是我认为最好的解决方案。但是你可以做些别的:
if (value_compared_to != LongFunctionName(first_argument,
another_argument,
finally_last_argument))
你有两个选择:
1)
接受它的样子
或
2)
在单独的语句(伪代码)中计算函数调用的返回值
retval = LongFunctioName(first_argument, second_argument, third_argument);
if(retval != desired_val) { ... }
不要让我们开始这个,我想
if
( LongFunctionName
( first_argument
, another_argument
, finally_last_argument
) != value_compared_to
)
{
// stuff to be called
}
相关文章:
- 函数调用中参数的顺序重要吗
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 变量没有改变?通过向量的函数调用
- 在两个类中共享相同的函数调用,并在不需要时避免空实例化
- 是否有C++编译器选项允许激进地删除所有函数调用,并将参数传递给具有空体的函数
- 我知道函数调用中存在歧义.有没有办法调用foo()函数
- 模板函数调用
- 获取从C++中同一类中的构造函数调用的方法返回的值
- 析构函数调用
- 成员函数调用和C++对象模型
- 使用共享指针的函数调用,其对象应为 const
- C++:编译时检查匹配的函数调用对?
- 函数调用C++中的参数太少
- 来自 DLL 的函数调用 [表观调用的括号前面的表达式必须具有(指向-)函数类型]
- 返回指向对象的指针的函数调用是否为 prvalue?
- C++ 如何重载 [] 运算符并进行函数调用
- 代码的效率. 转到和函数调用
- 是同一作用域的函数部分中的函数调用
- 如何封装一个函数,以便它只能由同一类中的一个其他函数调用?
- 长函数调用的可读性