In this specific case the person did so because he wanted to do it. He does not have a clear motivation and it should be so, so you are covered with reason. For the adepts of good practice, this is not wrong, but it is not good practice because it has created a more difficult flow deviation to follow, although for this example this is not so relevant, it would be when this code starts to grow. Indented code is usually more readable than a detour, so people say you should avoid goto
(not abolish), and the continue
is a form of goto
.
There could be a context that makes more sense than form without denial, but that’s not a bad example, but it’s questionable. This code would be better written like this:
function showPrimes(n) {
for (let i = 2; i < n; i++) if (isPrime(i)) console.log(i);
}
function isPrime(n) {
for (let i = 2; i < n; i++) if (n % i == 0) return false;
return true;
}
showPrimes(20);
I put in the Github for future reference.
There is a chain of people who advocate less flow control which is the example I posted, and there is another that advocates fewer execution blocks which is the example of the question. I argue that you should do what seems more obvious and readable, that shows more intent than you want. There are cases that try to avoid the continue
produces very weird and complicated codes to understand.
I’ve said some more on command in C and that I think gives a better idea of the continue
.
You know what the
continue
ago?– Woss
And why shouldn’t I? Why are you questioning that?
– Maniero
Worth reading the reference on the keyword
continue
.– Luiz Felipe
I understand the continue completely. The doubt is related to why the logic of the code was written in this way, since it is not as intelligible as doing the opposite.
– Pedro Matoso
I program similar to this case, also have an important observation that it is a good practice to write a true validation function in the case
isPrime()
instead ofnotIsPrime()
, and make denial at the time of validation. The use ofif
followed bycontinue
avoids a lot of code identation after several validations.– Leonardo Getulio
!isPrime(n)
."If it’s not the first one, "the person did it to avoid having to create another methodisNotPrime(n)
,isPrime
is a name of the importance of the method... it is being used as a reverse validation...– Ivan Ferrer