When coding, why is exiting a loop using break; bad?

  • Thread starter r.meghdies
  • Start date
  • #1
r.meghdies
When coding, why is exiting a loop using"break;" bad?

when coding, to exit a loop i was told using "break;" (in java) is really bad...why!
 

Answers and Replies

  • #2
27
3
It makes the code hard to follow. If the exit criteria is at the top of the loop, it is easy for the reader to figure out what is happening. However a break buried deep in a loop can do something surprising and surprises make code very difficult to follow. Then there are labeled breaks which act somewhat the same as a goto in C (more or less) and can quickly turn your code into an unreadable mess.

Breaks are always ok in switch statements. But elsewhere USE CAUTION!!!!

Remember any significant chunk of code will be maintained by someone else and that someone will want easy to read code not one riddled with hidden break statements.
 
Last edited:

Related Threads on When coding, why is exiting a loop using break; bad?

  • Last Post
Replies
6
Views
2K
  • Last Post
Replies
4
Views
1K
Replies
10
Views
10K
Replies
8
Views
696
Replies
2
Views
508
Replies
3
Views
1K
  • Last Post
Replies
6
Views
1K
  • Last Post
Replies
10
Views
838
Top