question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Use `while` for infinite loops

See original GitHub issue

I would like to propose the implementation and inclusion of a new StyleCop diagnostic.

Category: Readability Rule: Use while for infinite loops Description: Infinite loops can be expressed as either while (true) or for (;;). The former should always be used.

Background: Some older compilers, especially older non-optimizing C++ compilers or script compilers, produced sub-optimal code for a while (true) - e.g. they would emit code to evaluate the expression true followed by a conditional branch instruction. These same compilers would recognize the empty increment and condition blocks of a for (;;) statement, and emit an optimized unconditional branch instruction without any other evaluation. As a result, many highly-experienced developers who previously worked with these compilers have a habit of writing for (;;) in code. Developers coming from most other backgrounds tend to write while (true) instead.

Rationale: The C# compiler does not impose technical or performance limitations that restrict our ability to make a decision on this matter. As such, I believe that the decision should be made based on what the majority of C# developers would find easiest to read. I also believe this is while (true).

Analysis: There are several ways this could be implemented which impact the overall scope.

  1. This could be limited to locating specific cases of for(;;).
  2. This could be limited to locating cases of for(;{expression}?;).
  3. This could be limited to locating cases of any for statement which does not have an increment expression (the initializer, if any, could be placed before the while statement when converting to it). This introduces an additional challenge for a code fix because changing an initializer expression to a variable declaration statement changes the scope of a variable, and would need to handle any conflicts which arose.

Issue Analytics

  • State:open
  • Created 8 years ago
  • Reactions:1
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jackwhelptoncommented, Sep 17, 2015

I’ve always written an infinite loop as for (;;) just because it’s slightly more concise than the while (true) equivalent; I hadn’t realised this was unusual. For completeness, it’s worth noting that StyleCopAnalyzers raise a violation of SA1002 when you do this, which the original version never did.

I’ve raised https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/1489 for discussion of that.

0reactions
GregReddickcommented, Nov 20, 2015

Us old C hacks are used to for(;😉, but I think while(true) is easier to understand. I think 3 is the right choice. The code fix can change

for (int foo = 1;;)
{
}

to

int foo = 1;
while (true)
{
}

but if foo would cause a conflict, then wrap the whole thing in another set of braces. Presumably, since this is ugly, the programmer would then change the variable name. So the code fix would do this only in cases of conflict. This:

            for (int foo = 1; ;)
            {
                if (foo == 1)
                {
                    break;
                }
            }

            for (int foo = 1; foo < 10; foo++)
            {
                Console.WriteLine(foo);
            }

would turn into this:

            {
                int foo = 1;
                while (true)
                {
                    if (foo == 1)
                    {
                        break;
                    }
                }
            }

            for (int foo = 1; foo < 10; foo++)
            {
                Console.WriteLine(foo);
            }

The other alternative is to have the code fix refuse to operate if it would cause a problem.

Read more comments on GitHub >

github_iconTop Results From Across the Web

When implementing an infinite loop, is there a difference in ...
When implementing an infinite loop, is there a difference in using while(1) vs for(;;) vs goto ? ... Which infinite loop is faster...
Read more >
Infinite loops - a free Hacking with Swift tutorial
It's common to use while loops to make infinite loops: loops that either have no end or only end when you're ready. All...
Read more >
Infinite Loop in C
An infinite loop is a looping construct that does not terminate the loop and executes the loop forever. It is also called an...
Read more >
Python "while" Loops (Indefinite Iteration)
Iteration means executing the same block of code over and over, potentially many times. A programming structure that implements iteration is called a...
Read more >
Infinite Loops in Javascript
In while and do-while loops: · Ensure you have at least one statement within the loop that changes the value of the comparison...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found