ES.87:不要在条件语句中增加多余的==或!=
Doing so avoids verbosity and eliminates some opportunities for mistakes. Helps make style consistent and conventional.
这么做可以避免冗长的代码并且减少某些错误的机会。帮助提高代码的以执行并符合习惯。
Example(示例)
By definition, a condition in an if-statement, while-statement, or a for-statement selects between true and false. A numeric value is compared to 0 and a pointer value to nullptr.
从定义的角度来讲,if语句、while语句、for语句中的条件判断得到true或false的结果。数值和0比较,指针和nullptr进行比较。
// These all mean "if `p` is not `nullptr`"
if (p) { ... } // good
if (p != 0) { ... } // redundant `!=0`; bad: don't use 0 for pointers
if (p != nullptr) { ... } // redundant `!=nullptr`, not recommended
Often, if (p) is read as "if p is valid" which is a direct expression of the programmers intent, whereas if (p != nullptr) would be a long-winded workaround.
通常,if(p)被读作如果p是合法的,这是程序员意图的直接表达,而if(p != nullptr)却是一种冗长的表达方式。
Example(示例)
This rule is especially useful when a declaration is used as a condition
本规则在声明作为条件使用时特别有用。
if (auto pc = dynamic_cast(ps)) { ... } // execute if ps points to a kind of Circle, good
if (auto pc = dynamic_cast(ps); pc != nullptr) { ... } // not recommended
Example(示例)
Note that implicit conversions to bool are applied in conditions. For example:
注意可以隐式类型转换为布尔类型的运算都可以用于条件语句。例如S:
for (string s; cin >> s; ) v.push_back(s);
This invokes istream's operator bool().
这段代码利用了istream的bool()运算符。
Note(注意)
Explicit comparison of an integer to 0 is in general not redundant. The reason is that (as opposed to pointers and Booleans) an integer often has more than two reasonable values. Furthermore 0 (zero) is often used to indicate success. Consequently, it is best to be specific about the comparison.S:
将整数和0进行显示比较通常不是冗长形式。原因是(和指针和布尔类型不同,)整数通常可以表达多于两个有意义的值。另外通常使用0(zero)表示成功。因此,最好将整数比较作为特例。
void f(int i)
{
if (i) // suspect
// ...
if (i == success) // possibly better
// ...
}
Always remember that an integer can have more than two values.
一定记住整数可以拥有的有效值可以超过两个。
Example, bad(反面示例)
It has been noted that
已经提醒过了:
if(strcmp(p1, p2)) { ... } // are the two C-style strings equal? (mistake!)
is a common beginners error. If you use C-style strings, you must know the
这是一个常见的,初学者错误。如果你使用C风格字符串,以一定知道
if(strcmp(p1, p2) != 0) { ... } // are the two C-style strings equal? (mistake!)
would not in itself save you.
也没什么帮助。
Note(注意)
The opposite condition is most easily expressed using a negation:
使用!更容易表达反逻辑:
// These all mean "if `p` is `nullptr`"
if (!p) { ... } // good
if (p == 0) { ... } // redundant `== 0`; bad: don't use `0` for pointers
if (p == nullptr) { ... } // redundant `== nullptr`, not recommended
Enforcement(实施建议)
Easy, just check for redundant use of != and == in conditions.
容易,只需要检查条件语句中多余的!=和==。
原文链接
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#es87-dont-add-redundant--or--to-conditions
新书介绍
以下是本人3月份出版的新书,拜托多多关注!
本书利用Python 的标准GUI 工具包tkinter,通过可执行的示例对23 个设计模式逐个进行说明。这样一方面可以使读者了解真实的软件开发工作中每个设计模式的运用场景和想要解决的问题;另一方面通过对这些问题的解决过程进行说明,让读者明白在编写代码时如何判断使用设计模式的利弊,并合理运用设计模式。
对设计模式感兴趣而且希望随学随用的读者通过本书可以快速跨越从理解到运用的门槛;希望学习Python GUI 编程的读者可以将本书中的示例作为设计和开发的参考;使用Python 语言进行图像分析、数据处理工作的读者可以直接以本书中的示例为基础,迅速构建自己的系统架构。
觉得本文有帮助?欢迎点赞并分享给更多的人。
阅读更多更新文章,请关注微信公众号【面向对象思考】