前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++核心准则ES.87:不要在条件语句中增加多余的==或!=

C++核心准则ES.87:不要在条件语句中增加多余的==或!=

作者头像
面向对象思考
发布2020-06-17 19:05:37
6000
发布2020-06-17 19:05:37
举报

ES.87: Don't add redundant == or != to conditions

ES.87:不要在条件语句中增加多余的==或!=

Reason(原因)

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进行比较。

代码语言:javascript
复制
// 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

本规则在声明作为条件使用时特别有用。

代码语言:javascript
复制
if (auto pc = dynamic_cast<Circle>(ps)) { ... } // execute if ps points to a kind of Circle, good

if (auto pc = dynamic_cast<Circle>(ps); pc != nullptr) { ... } // not recommended
Example(示例)

Note that implicit conversions to bool are applied in conditions. For example:

注意可以隐式类型转换为布尔类型的运算都可以用于条件语句。例如S:

代码语言:javascript
复制
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)表示成功。因此,最好将整数比较作为特例。

代码语言:javascript
复制
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

已经提醒过了:

代码语言:javascript
复制
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 <cstring> functions well. Being verbose and writing

这是一个常见的,初学者错误。如果你使用C风格字符串,以一定知道<cstring>函数。保持冗长并书写

代码语言:javascript
复制
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:

使用!更容易表达反逻辑:

代码语言:javascript
复制
// 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

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-06-10,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 面向对象思考 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Reason(原因)
    • Example(示例)
      • Enforcement(实施建议)
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档