Why should we minimize the use of interruption and continue the loop?

When I was a beginner, our instructor allowed us to use the break or continue loop. I did this most of the time when it completes / continues the cycle. And now I'm in my second year, my instructor told me that using break / continue not practical. Can you tell me why? What influences the gap / continuation?

+4
source share
3 answers

Some people find it too bad to have too much control flow, which means things like break , continue and multiple return s. The reason is not technical, but basically this complex flow of control can complicate the verification, validation and justification of the program.

This is mainly a matter of style, personal taste and your overall structure. With small, well-oriented features, there can be little harm if there are several possible threads. In C ++, in particular, early exit is a popular idiom, and can often make code more convenient.

+17
source

At least in C you shouldn't use break and / or continue β€œmost of the time” (as your question says what you did) to control the flow of your loops. The loop condition should indicate under what circumstances the loop should stop; someone supporting your code should not dig up the code in the body of your loop to find out what causes break , which causes the loop to stop.

For example, let's say you want to read an integer from an inputFile to find out if one of the integers is 500. One way to structure a loop is:

 while (fgets (buffer, sizeof (buffer), inputFile)){ sscanf (buffer, "%d", &num); if (num == 500) break; } 

Here, the person reading your code should read the entire while to find out what you are really looking for in the file. If you write this without break :

 while ((num != 500) && fgets (buffer, sizeof (buffer), inputFile)) sscanf (buffer, "%d", &num); 

the loop condition itself tells the reader exactly what your code is trying to do, which makes it much easier to understand quickly. In addition, as a bonus, you have saved several lines of code.

Now imagine a more complex while or for while , where break is deep inside the loop body. It's easy to see why trying to find a break trigger will be annoying. The presence of a correctly structured loop condition is much larger, but also self-documenting.

Of course, there are times when break and continue are actually good ways to write code. For example, if the condition in which the loop should end may occur in the middle of the loop, and there is a long set of statements that follow inside the loop, and the execution of these statements will add processing time without doing anything useful, and use break . But these cases are an exception, not "most of the time."

+3
source

Most of the logic that I saw for reasoning about the correctness of the code allows for single input / single output. If your loops are filled with break and continue , it becomes impossible to know whether your invariants of the loop are executed or whether you are always progressing (therefore, the loop will not be infinite). (Note that the do { ... } while (...); also suffers from this: the loop invariants are not set for the first time, which may lead to some surprises.)

In most cases, when you try to use break or continue , you probably made the loop (and the function that contains it) too big and too complicated.

Some argue that something like:

 for (;;) { // ... if ( conditionMet ) { break; } // ... } 

will be acceptable for the classic loop and half idiom; This is a single entry / only exit, after all, even if the exit is not where we expect it (and it is very difficult to find when you read the code). The problem remains regarding the cycle invariants; not executed until if , at least for the first time. Typically, the best solution would be to put the code before the test in a separate function that returns a conditionMet , and Application:

 while ( doLoopPrefix() ) { doLoopSuffix(); } 

(In general, if your loop is longer than three or four lines, you should refactor. Also, if it contains one switch with a lot of cases.)

+3
source

Source: https://habr.com/ru/post/1500270/


All Articles