Should I avoid using Java Label Statements?

前端 未结 11 1456
北荒
北荒 2020-11-29 04:27

Today I had a coworker suggest I refactor my code to use a label statement to control flow through 2 nested for loops I had created. I\'ve never used them before because per

11条回答
  •  情深已故
    2020-11-29 05:05

    Labels are like goto's: Use them sparingly, and only when they make your code faster and more importantly, more understandable,

    e.g., If you are in big loops six levels deep and you encounter a condition that makes the rest of the loop pointless to complete, there's no sense in having 6 extra trap doors in your condition statements to exit out the loop early.

    Labels (and goto's) aren't evil, it's just that sometimes people use them in bad ways. Most of the time we are actually trying to write our code so it is understandable for you and the next programmer who comes along. Making it uber-fast is a secondary concern (be wary of premature optimization).

    When Labels (and goto's) are misused they make the code less readable, which causes grief for you and the next developer. The compiler doesn't care.

提交回复
热议问题