【发布时间】:2019-10-06 04:35:35
【问题描述】:
我正在使用 CLion IDE 对我的 C++ 项目进行编码。有时,IDE 会尝试比我更聪明并给我建议。我在代码检查期间遇到了一个简单的问题(由 CLion 提供)。它说可以简化以下代码,尽管我相信这是我能想到的最简单的形式:
代码:
if (node.first >= 0 && node.first <= 45 &&
node.second >= 0 && node.second <= 30)
return true;
else
return false;
假设节点的类型为std::pair<int, int>
我从 CLion IDE 得到的建议如下:
代码检查 cmets:
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.
你认为这可以更简化吗?
【问题讨论】:
-
您有
else子句吗? -
您确定成员不是无符号类型?
-
也许可以推断出这些条件之一总是/从不成立?
-
为什么“假设”节点的类型是 std::pair
? -
这些检查通常带有将代码重构为替代形式的选项。你确定它没有给你那个选择吗?其实it does for me with this example,确实改写成答案中的形式了。
标签: c++ if-statement conditional-statements std-pair code-inspection