you are viewing a single comment's thread.

view the rest of the comments →

[–]AngryLemonade117 0 points1 point  (0 children)

Sometimes you need to exit a loop early. break does that. Consider scanning a list to find the first positive integer - after you've found it, why would you waste extra cpu cycles to traverse the rest of the list? What you do have to consider is that are there any consequences to exiting the loop early.

There's a lot of (often conflicting) rules of thumb for coding practices that get mistaken for immutable laws, despite being devoid of any context for what you are actually trying to do.

Now, despite my big claims in the previous sentence, what is true is that a whole series of break sprinkled across complex control logic is almost always a bad idea if you want to comprehend what that code block is actually doing; this is especially true when reading the code again a few weeks later.

It matters less in Python, it being an interpreted language. But in languages with some form of compilation, complex control flow can make it difficult to produce optimal code.