0
点赞
收藏
分享

微信扫一扫

C++核心准则ES.73:如果没有明显的循环变量,while语句要好于for语句


ES.73: Prefer a while-statement to a for-statement when there is no obvious loop variable

ES.73:如果没有明显的循环变量,while语句要好于for语句

 

Reason(原因)

Readability.

可读性

 

Example(示例)

int events = 0;
for (; wait_for_event(); ++events) { // bad, confusing
// ...
}

The "event loop" is misleading because the events counter has nothing to do with the loop condition (wait_for_event()). Better

因为event计数和循环条件(wait_for_event())没有任何关系,“event loop”实际上是一种误导。较好的写法是:

int events = 0;
while (wait_for_event()) { // better
++events;
// ...
}

 

Enforcement(实施建议)

Flag actions in for-initializers and for-increments that do not relate to the for-condition.

如果循环变量初始化和增量操作中的操作和循环条件没有任何关系,进行提示。

 

原文链接

​​https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#es73-prefer-a-while-statement-to-a-for-statement-when-there-is-no-obvious-loop-variable​​

 

 

新书介绍

以下是本人3月份出版的新书,拜托多多关注!

 

C++核心准则ES.73:如果没有明显的循环变量,while语句要好于for语句_while

本书利用Python 的标准GUI 工具包tkinter,通过可执行的示例对23 个设计模式逐个进行说明。这样一方面可以使读者了解真实的软件开发工作中每个设计模式的运用场景和想要解决的问题;另一方面通过对这些问题的解决过程进行说明,让读者明白在编写代码时如何判断使用设计模式的利弊,并合理运用设计模式。

对设计模式感兴趣而且希望随学随用的读者通过本书可以快速跨越从理解到运用的门槛;希望学习Python GUI 编程的读者可以将本书中的示例作为设计和开发的参考;使用Python 语言进行图像分析、数据处理工作的读者可以直接以本书中的示例为基础,迅速构建自己的系统架构。

 

觉得本文有帮助?欢迎点赞并分享给更多的人。

阅读更多更新文章,请关注微信公众号【面向对象思考】

举报

相关推荐

0 条评论