C++核心准则ES.75: 避免使用do语句

2020-06-03 11:04:53 浏览数 (1)

ES.75: Avoid do-statements

ES.75: 避免使用do语句

Reason(原因)

Readability, avoidance of errors. The termination condition is at the end (where it can be overlooked) and the condition is not checked the first time through.

可读性,避免错误。中止条件位于循环的最后(可能被忽视的位置),并且第一次进入循环时不会检查循环条件

Example(示例)

代码语言:javascript复制
int x;
do {
    cin >> x;
    // ...
} while (x < 0);
Note(注意)

Yes, there are genuine examples where a do-statement is a clear statement of a solution, but also many bugs.

确实存在使用do语句的清晰易懂的例子,但同时也存在很多错误。

Enforcement(实施建议)

Flag do-statements.

标记使用do语句的代码。

原文链接

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#es75-avoid-do-statements

0 人点赞