C++核心準(zhǔn)則ES.73:如果沒有明顯的循環(huán)變量,while語句要好于for語句

ES.73: Prefer a?while-statement to a?for-statement when there is no obvious loop variable
ES.73:如果沒有明顯的循環(huán)變量,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
因?yàn)閑vent計(jì)數(shù)和循環(huán)條件(wait_for_event())沒有任何關(guān)系,“event loop”實(shí)際上是一種誤導(dǎo)。較好的寫法是:
int events = 0;
while (wait_for_event()) { // better
++events;
// ...
}
Enforcement(實(shí)施建議)
Flag actions in?for-initializers and?for-increments that do not relate to the?for-condition.
如果循環(huán)變量初始化和增量操作中的操作和循環(huán)條件沒有任何關(guān)系,進(jìn)行提示。
原文鏈接
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
覺得本文有幫助?請分享給更多人。
關(guān)注微信公眾號【面向?qū)ο笏伎肌枯p松學(xué)習(xí)每一天!
面向?qū)ο箝_發(fā),面向?qū)ο笏伎迹?/span>
評論
圖片
表情
