简体   繁体   English

if(!result)返回false的C ++糖语法;

[英]C++ sugar syntax for if (!result) return false;

When refactoring some code, I often encounter this : 重构某些代码时,我经常会遇到以下问题:

bool highLevelFunc foo()
{
  // ...
  bool result = LesserLevelFunc();
    if (!result) return false;
  // ... Keep having fun if we didn't return
}

Is there any way to make this a little more sexy and less verbose ? 有什么办法可以使它更加性感和不那么冗长? Without any overhead or pitfall of course. 当然没有任何开销或陷阱。

I can think of a macro 我可以想到一个宏

#define FORWARD_IF_FALSE(r) if (!r) return r;

bool highLevelFunc foo()
{
  // ...
  FORWARD_IF_FALSE(LesserLevelFunc());
  // ...
}

Anything better, ie without preprocessor macro? 还有没有更好的东西,即没有预处理器宏?

To me, "readable" code is sexy. 对我来说,“可读”代码很性感。 I find the original code more readable than your proposal, since the original uses standard C++ syntax and the latter uses a macro which I'd have to go and look up. 我发现原始代码比您的建议更具可读性,因为原始代码使用标准的C ++语法,而后者则使用了我必须查找的宏。

If you want to be more explicit, you could say if (result == false) (or better yet, if (false == result) to prevent a possible assignment-as-comparison bug) but understanding the ! 如果您想更加明确,可以说出if (result == false) (或者更好的是if (false == result)以防止可能的赋值比较错误),但请理解! operator is a fairly reasonable expectation in my opinion. 我认为运算符是一个合理的期望。

That said, there is no reason to assign the return value to a temporary variable; 也就是说,没有理由将返回值分配给临时变量。 you could just as easily say: 您可以轻松地说:

if (!LesserLevelFunc()) return false;

This is quite readable to me. 这对我来说很容易理解。

EDIT : You could also consider using exceptions instead of return values to communicate failure. 编辑 :您还可以考虑使用异常而不是返回值来传达故障。 If LesserLevelFunc() threw an exception, you would not need to write any special code in highLevelFunc() to check for success. 如果LesserLevelFunc()抛出异常,则无需在highLevelFunc()编写任何特殊代码即可检查是否成功。 The exception would propagate up through the caller to the nearest matching catch block. 异常会通过调用者向上传播到最接近的匹配catch块。

Because you might be continuing if LesserLevelFunc returns true, I suggest keeping it pretty close to how it is now: 因为如果LesserLevelFunc返回true,您可能会继续,所以建议将其保持与现在的状态非常接近:

if (!LesserLevelFunc())
    return false;

First of all introducing the macro you are making the code unsafe. 首先介绍宏,这会使代码不安全。 Moreover your macro is invalid. 此外,您的宏无效。

The expression after the negation operator shall be enclosed in parentheses. 取反运算符后的表达式应放在括号中。

#define FORWARD_IF_FALSE(r) if (!( r ) ) return r;

Secondly the macro calls r twice. 其次,宏调用两次r。 Sometimes two calls of a function is not equivalent to one call of the same function. 有时,一个函数的两次调用不等同于同一函数的一次调用。 For example the function can have some side effects or internal flags that are switched on/off in each call of the function. 例如,该函数可能具有一些副作用或在每次调用该函数时打开/关闭的内部标志。

So I would keep the code as is without introducing the macro because the macro does not equivalent to the symantic of the original code. 因此,我将在不引入宏的情况下将代码保持原样,因为该宏不等同于原始代码的对称性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM