Is while (true) with break bad programming practice?

后端 未结 22 2186
-上瘾入骨i
-上瘾入骨i 2020-11-27 04:38

I often use this code pattern:

while(true) {

    //do something

    if() {
        break;
    }

}   

Another progr

相关标签:
22条回答
  • 2020-11-27 04:42

    There is a discrepancy between the two examples. The first will execute the "do something" at least once every time even if the statement is never true. The second will only "do something" when the statement evaluates to true.

    I think what you are looking for is a do-while loop. I 100% agree that while (true) is not a good idea because it makes it hard to maintain this code and the way you are escaping the loop is very goto esque which is considered bad practice.

    Try:

    do {
      //do something
    } while (!something);
    

    Check your individual language documentation for the exact syntax. But look at this code, it basically does what is in the do, then checks the while portion to see if it should do it again.

    0 讨论(0)
  • 2020-11-27 04:42

    What your friend recommend is different from what you did. Your own code is more akin to

    do{
        // do something
    }while(!<some condition>);
    

    which always run the loop at least once, regardless of the condition.

    But there are times breaks are perfectly okay, as mentioned by others. In response to your friend's worry of "forget the break", I often write in the following form:

    while(true){
        // do something
    if(<some condition>) break;
        // continue do something
    }
    

    By good indentation, the break point is clear to first time reader of the code, look as structural as codes which break at the beginning or bottom of a loop.

    0 讨论(0)
  • 2020-11-27 04:43

    No, that's not bad since you may not always know the exit condition when you setup the loop or may have multiple exit conditions. However it does require more care to prevent an infinite loop.

    0 讨论(0)
  • 2020-11-27 04:44

    He is probably correct.

    Functionally the two can be identical.

    However, for readability and understanding program flow, the while(condition) is better. The break smacks more of a goto of sorts. The while (condition) is very clear on the conditions which continue the loop, etc. That doesn't mean break is wrong, just can be less readable.

    0 讨论(0)
  • 2020-11-27 04:45

    If there's one (and only one) non-exceptional break condition, putting that condition directly into the control-flow construct (the while) is preferable. Seeing while(true) { ... } makes me as a code-reader think that there's no simple way to enumerate the break conditions and makes me think "look carefully at this and think about carefully about the break conditions (what is set before them in the current loop and what might have been set in the previous loop)"

    In short, I'm with your colleague in the simplest case, but while(true){ ... } is not uncommon.

    0 讨论(0)
  • A few advantages of using the latter construct that come to my mind:

    • it's easier to understand what the loop is doing without looking for breaks in the loop's code.

    • if you don't use other breaks in the loop code, there's only one exit point in your loop and that's the while() condition.

    • generally ends up being less code, which adds to readability.

    0 讨论(0)
提交回复
热议问题