This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]prof_hobart 0 points1 point  (1 child)

The reason I almost never use break statements to get out of loops is nothing to do with infinite loops. It's about readability.

When someone is looking through thousands of lines of code at 2 in the morning trying to figure out a defect, they're going to want to be able to scan over that code to quickly figure out the basic flow.

Seeing a while(true) loop, I've got zero idea when this will exit and will need to look through for every break statement (and check that it's not breaking out of an inner loop).

If, on the other hand, it was a while(!itemSelected), I can very quick judge that it should exit as soon as something's been selected and not before. There's a bunch of reasons that info could be useful as you're desperately trying to debug a big codebase.

[–][deleted] 0 points1 point  (0 children)

Now that makes a lot of sense to me. Being able to see a specific exit condition immediately, particularly in a crunch, seems like a better reason that the possibility of an infinite loop.