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

面向对象思考

共 710字,需浏览 2分钟

 · 2020-06-02

34f9bc09653f5184bba452a4d038ae11.webp

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(示例)

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




觉得本文有帮助?请分享给更多人。

关注微信公众号【面向对象思考】轻松学习每一天!

面向对象开发,面向对象思考!

浏览 42
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报