26

I have come JS code with some infinite loops (we're using node-fibers to allow things to happen).

ESLint hates:

while (true) {

}

because of the constant condition.

The following is allowed though:

for(;;) {

}

Beyond just feeding the lintbeast, are there any objective reasons to favour for over while (or vice versa)?

NOTE: This question is explicitly requesting objective reasons and so is not simply opinion based.

Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
Dancrumb
  • 26,597
  • 10
  • 74
  • 130
  • 2
    Since performance is not usually a concern in infinite loops... who cares? – ssube May 26 '16 at 16:36
  • Is this with vanilla eslint or with plugins? (never used it myself) – glcheetham May 26 '16 at 16:37
  • 10
    Not sure why so many are downvoting, this seems like a legitimate question. – dmeglio May 26 '16 at 16:37
  • 2
    @ssube well performance isn't the only consideration in a well formed program... so I (and potentially others) care. Hence the question. – Dancrumb May 26 '16 at 16:38
  • @glcheetham vanilla eslint. There's a rule (http://eslint.org/docs/rules/no-constant-condition) that trips on this. I could disable the rule, but then I'd miss more subtle and inappropriate constant conditions. – Dancrumb May 26 '16 at 16:38
  • My guess would be, `for (;;)` is more obvious that it was intentional than is `while (true)`. Personally I'd do whatever is more readable. – dmeglio May 26 '16 at 16:40
  • 2
    This looks like an opinion based question. The while loop is the convention most people use, because it reads in a spoken word fashion. Ie "while true" is universally known to indicate an infinite loop, "for semicolon semicolon" does not immediately indicate that. – Bryce Drew May 26 '16 at 16:41
  • 1
    @BryceDrew the question is not "is it a good practice", it's "why does this particular library hold that opinion" which is in fact _not_ opinion based. If you read my answer - there is also nothing opinion based about it. – Benjamin Gruenbaum May 26 '16 at 16:48
  • 1
    @Dancrumb that was largely sarcasm. This still fits the definition of an opinion-based question. – ssube May 26 '16 at 16:48

1 Answers1

11

These rules about infinite loops are from before generators were a thing and are not even aware of fibers.

Under the assumption that every function never suspends and returns (like a generator, async-keyword function or a fiber) the rule makes a lot of sense to warn against constants in loops.

Now that times have changed - the rule no longer makes sense and what you're doing is perfectly fine.

If we check the eslint repo it was discussed and deemed "not important enough to acknowledge" in the meantime:

I don't think this makes sense as a built-in exception. If you're doing that, then it's best to manually disable the rule in the generator using a comment.

The workaround for(;;) was suggested but everyone involved understands it's a hack for this particular case.

Disable the rule.

Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504