C++核心准则ES.73:如果没有明显的循环变量,while语句要好于for语句
面向对象思考
共 916字,需浏览 2分钟
·
2020-05-31 23:21
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
觉得本文有帮助?请分享给更多人。
关注微信公众号【面向对象思考】轻松学习每一天!
面向对象开发,面向对象思考!
评论