对于可能在结束之前中断的迭代使用什么循环?
我有一系列内存需要解析。如果我在结束之前找到某个字节序列,我会中断迭代。我想知道我应该在这里更喜欢哪个循环:
while(i < end && !sequenceFound ) {
// parse
i++;
}
或者
for( i; i < end && !sequenceFound; i++ ) {
// parse
}
这是在派生自实现环形缓冲区的类的类的方法中使用的。超类提供了i
和end
。我的问题是,您认为对于不熟悉代码的人来说哪一个更容易理解(更好地表达意图)?
编辑 事实上,我发现该序列是进一步解析流所必需的。我可以使用 break
并设置 sequenceFound = true
,但这会是多余的,还是我在这里太严格了?
I have a range of memory to parse. If I find a certain sequence of bytes before the end, I interrupt the iteration. I wonder which loop I should prefer here:
while(i < end && !sequenceFound ) {
// parse
i++;
}
Or
for( i; i < end && !sequenceFound; i++ ) {
// parse
}
This is used in a method of a class that derives from a class that implements a ring buffer. The superclass provides i
and end
. My question is, which one do you think is easier to understand (expresses the intend better) for someone unfamiliar with the code?
Edit The fact that I found the sequence is needed for the further parsing of the stream. I could use break
and set sequenceFound = true
, but that would be redundant, or am I being to strict here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
为什么不在需要“中断”循环时使用
break;
呢?这似乎是最能惯用地表达您的意图的语言功能。这通常意味着您可以不需要额外的布尔状态跟踪变量。如果您需要知道迭代是否提前终止,可以使用条件
i != end。
无论哪种方式,使用最清晰的控制方法似乎都是最好的,并且break;
无论你是否保持“提前退出”变量,在我看来,你想要打破的时间点似乎是最清楚的。继续循环并测试您刚刚保证会失败的条件似乎是多余的。Why not just use
break;
at the point that the need to "interrupt" the loop is encountered. This seems like the language feature that most idiomatically expresses your intent. It usually means that you can do without the extra boolean state tracking variable.If you need to know whether the iteration terminated early you can use the condidition
i != end.
Either way, using the control method that is clearest would seem best andbreak;
at the point at which you want to break seems clearest to me, whether or not you maintain an "early exit" variable. It seems redundant to carry on round the loop and test a condition that you've only just guaranteed will fail.更喜欢算法而不是手写循环。
Prefer algorithms to hand-written loops.
对于具有明确限制的迭代,我通常会使用 for 循环。当迭代次数没有上限时,应使用 while 循环。
For an iteration that has an explicit limit, I would normally use a for loop. While loops should be used when the iteration has no upper limit.
对于具有循环变量和结束的循环(即,对于通过检查
i != end
或i < end
在一定范围内运行的所有循环),我喜欢for
方法,因为这或多或少是for
的规范用法。如果您可能会过早离开循环,请
break
:For loops with a loop variable and an end (i.e. for all loops that run over a range by checking
i != end
ori < end
) I favour thefor
approach since this is more or less the canonical usage offor
.If you may leave the loop prematurely,
break
:我更喜欢这里的
while
因为这样读起来更好。不过可能是个人喜好。until
在某些语言中可用也很好。I favour
while
here as that reads better. Might be a personal preference though.until
which is available in some languages would be good as well.第二个版本更容易理解并允许使用 continue 运算符。
The second version is better for understanding and allows to use continue operator.
如果我在每次迭代之前要检查多个条件,我通常会使用 while 循环。
If I have more than one condition to check before each iteration I usually use a while loop.
同意 Charles Bailey 的观点,因为一旦你中断迭代,你就会立即脱离最直接的循环结构,而不必重新评估条件变量。此外,您不需要额外的条件变量,从而减少(稍微减少)代码块运行所需的内存。
Agree with Charles Bailey as once you break in the iteration, you immediatley break out of the most immediate looping structure, without having to re-evaluate a condition variable. Also, you do not need the additional condition variable, thus reducing (slightly that is) memory required for the block of code to run.
我更喜欢内部带有
break
的for
。原因是您可以避免必须检查“中断”条件的丑陋构造,例如:
但是使用 while 版本或带有内置条件的 for ,当然取决于您正在做什么,您可以'不要那么轻易地避免这个条件。
I prefer the
for
with thebreak
internally.The reason being that you can avoid ugly constructs that have to check your 'break' condition, e.g.:
But with either the while version or the for with the condition built-in, depending of course on what you're doing, you can't so easily avoid that conditional.