Is while (true) with break bad programming practic

2020-01-24 11:56发布

I often use this code pattern:

while(true) {

    //do something

    if(<some condition>) {
        break;
    }

}   

Another programmer told me that this was bad practice and that I should replace it with the more standard:

while(!<some condition>) {

    //do something

}   

His reasoning was that you could "forget the break" too easily and have an endless loop. I told him that in the second example you could just as easily put in a condition which never returned true and so just as easily have an endless loop, so both are equally valid practices.

Further, I often prefer the former as it makes the code easier to read when you have multiple break points, i.e. multiple conditions which get out of the loop.

Can anyone enrichen this argument by adding evidence for one side or the other?

22条回答
萌系小妹纸
2楼-- · 2020-01-24 12:42

Sometime you need infinite loop, for example listening on port or waiting for connection.

So while(true)... should not categorized as good or bad, let situation decide what to use

查看更多
来,给爷笑一个
3楼-- · 2020-01-24 12: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.

查看更多
Viruses.
4楼-- · 2020-01-24 12:44

while (true) might make sense if you have many statements and you want to stop if any fail

 while (true) {
     if (!function1() ) return;   
     if (!function2() ) return;   
     if (!function3() ) return;   
     if (!function4() ) return;  
   }

is better than

 while (!fail) {
     if (!fail) {
       fail = function1()
     }           
     if (!fail) {
       fail = function2()
     }  
     ........

   }
查看更多
孤傲高冷的网名
5楼-- · 2020-01-24 12:44

I prefer the while(!) approach because it more clearly and immediately conveys the intent of the loop.

查看更多
神经病院院长
6楼-- · 2020-01-24 12:46

using loops like

while(1) { do stuff }

is necessary in some situations. If you do any embedded systems programming (think microcontrollers like PICs, MSP430, and DSP programming) then almost all your code will be in a while(1) loop. When coding for DSPs sometimes you just need a while(1){} and the rest of the code is an interrupt service routine (ISR).

查看更多
The star\"
7楼-- · 2020-01-24 12:47

The first is OK if there are many ways to break from the loop, or if the break condition cannot be expressed easily at the top of the loop (for example, the content of the loop needs to run halfway but the other half must not run, on the last iteration).

But if you can avoid it, you should, because programming should be about writing very complex things in the most obvious way possible, while also implementing features correctly and performantly. That's why your friend is, in the general case, correct. Your friend's way of writing loop constructs is much more obvious (assuming the conditions described in the preceding paragraph do not obtain).

查看更多
登录 后发表回答