18

So my dear programming teacher really hates break statements... I mean really really really. He thinks it's better for readability if you don't break from any kinds of loops (not even ifs) well then we came across a switch statement in class. He says "breaks only exist because it's needed in switches" well how about returning from a fcking swith? or goto? then you need no break...
Is there anyone who could explain why I should NEVER use breaks and why it's bad in any piece of code? Why is it better to just use whiles because fors are apparently evil again? Srsly I just wanna ask him to show me some big code bases without breaks...

Comments
  • 3
    He's stupid.
    Breaks aren't always stupid. Of course, you shouldn't use it in nested loops, but they have their proper use-cases.
  • 5
    But hell no don't use goto, it's the end of code readability.
  • 1
    @filthyranter well in java you have labels for loops so you can break to a specific loop but at school we learn c#...
  • 4
    break, continue, and return all have their uses; some uses are better than others. And there is always someone smarter than you who can refactor them away.
  • 2
    I don't know the context, you'd like to reduce nesting and continues, but ideally as long as you're not using goto, I'd be mostly fine with it as long as it can't be rewritten in simpler manner with or without break.
  • 2
    @bkwilliams I love those words. There’s always someone smarter than you who can refactor. Truth.

    As long as they teach me what they did though haha
  • 1
    I thought putting breaks was a bad practice. In most cases is just bad logic from the programmer.
  • 1
    @HelloIsItMe I very much agree. There are some cases where breaks make sense, but thank god for the beauty that is `continue`. I find I use both of them the most when I am writing parsing code, as for some kinds of data the best way to handle invalid data seems to me (I’m self-taught, so take with a grain of salt) to be to ignore it and move on. Better to have something kinda work than crap out on the user.
Add Comment