if语句如何简化?

Aru*_*mar 1 c++ if-statement code-inspection conditional-statements std-pair

我正在使用CLion IDE编码我的C ++项目。有时候,IDE会比我更聪明,并给我一些建议。在代码检查过程中(CLion),我遇到一个简单的问题。它说以下代码可以简化,即使我认为这是我能想到的最简单的形式:

代码:

    if (node.first >= 0 && node.first <= 45 &&
    node.second >= 0 && node.second <= 30)
    return true;
    else
    return false;
Run Code Online (Sandbox Code Playgroud)

假设节点的类型 std::pair<int, int>

我从CLion IDE获得的建议如下:

代码检查注释:

Inspection info: This inspection finds the part of the code that can be simplified, e.g. constant conditions, identical if branches, pointless boolean expressions, etc.
Run Code Online (Sandbox Code Playgroud)

您认为这可以进一步简化吗?

Sto*_*ica 6

CLion暗示您这一点...

if (node.first >= 0 && node.first <= 45 &&
    node.second >= 0 && node.second <= 30)
    return true;
else
    return false;
Run Code Online (Sandbox Code Playgroud)

可以改写成

return node.first  >= 0 && node.first  <= 45 &&
       node.second >= 0 && node.second <= 30;
Run Code Online (Sandbox Code Playgroud)

由于在控制语句中用作条件的表达式显然具有对true和false的自然转换。