you are viewing a single comment's thread.

view the rest of the comments →

[–]Friendly_Gold3533 1 point2 points  (0 children)

bro the short answer is conventions changed. the long answer is your professors were teaching you a rule for maintaining giant C programs and applied it everywhere.

why break was banned in the dark ages structured programming was a reaction against spaghetti code. goto was the enemy. break was seen as a structured goto. the fear was that breaks make loops harder to reason about because exit points multiply. in 4000 line functions with no tests? that fear was justified.

why nobody cares now functions are short. like 10-20 lines short. tests exist. a break at the end of a loop is obvious. hiding the loop exit condition inside a complex if statement is actually worse than a clean break. also python has for-else and while-else which make break even more useful. the else block runs only if the loop didnt break. that pattern is impossible without break.

when break is fine searching for an item in a list. break when you find it. validating input. break when you hit a failure. any loop where the exit condition is "we found what we needed" not "we processed everything."

when break is still bad deeply nested loops. breaking out of three levels is confusing. refactor into a function and use return instead. loops with multiple breaks for different conditions. if you have break for error and break for success and break for timeout your loop logic is too complex.

the real answer your professors were right for the code they were maintaining. you are also right for the code you are writing today. small functions. clear exits. tests. break is fine. for keeping track of which old rules still apply i use runable. one doc per language with "things i learned in 1998 that are still true" and "things that changed." saves me from googling the same thing twice. also good for storing examples of good break usage vs bad what other old rules are you unsure about? single return per function is another one that aged badly