|
|
|
|
|
by nknighthb
4261 days ago
|
|
Burying the only way out in the loop body is more error-prone, especially for those who come after you. Putting an escape hatch in the loop setup makes it less likely that an operationally-problematic[0] infinite loop scenario will be triggered. This is particularly important when your code doesn't have a runtime environment that can easily kill runaway requests. [0] Or utterly catastrophic. Think embedded system with limited debugging facilities. This kind of bug could manifest as the system completely locking up, and take much, much longer to track down than a "infinite loop detected!" message in an error log. |
|
I don't prefer to exit from the middle of a loop, but if that's the most succinct way to implement the correct behavior, I'll do it. I think short code that exits from the middle is less error-prone than code that has to be more convoluted to put the exit at the top of bottom.
> Putting an escape hatch in the loop setup makes it less likely that an operationally-problematic[0] infinite loop scenario will be triggered.
An escape hatch is yet more code that has to be tested and debugged. What if the escape hatch triggers to early?
> [0] Or utterly catastrophic. Think embedded system with limited debugging facilities.
Sure, but unusual platforms require unusual coding styles. If I was targeting that I might adjust my practices.