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.

Feature Request: Short Syntax For Empty While Loop

See original GitHub issue

I’d like to write while (!(this.done && other.done)); instead of while (!(this.done && other.done)) {} for non-blocking threaded code, where the while loop is often used to wait for a value to compute and doesn’t actually contain anything besides the head. This would simply be for more code clarity, nothing else.

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
zamfofexcommented, Sep 11, 2017

I feel like it’s much clearer that while is waiting for something to happen instead of making it happen itself when the curly braces are actually there. I’d even put them in their own line:

while(!done)
{
}
0reactions
phlopsicommented, Sep 17, 2017

Wouldn’t you want to check the actual value if the reference didn’t had the expected value?

If you didn’t compare and set the value in a single operation or using a lock, the condition can succeed, then the scheduler could switch the context, that other context could do something that’d cause the condition to return false, then the scheduler switches back to the previous context and executes code while the condition is actually false and not true, which may lead to unwanted behaviour.

To prevent race conditions you either need to lock parts of code or you use atomic operations. Atomic operations reflect CPU commands, so they don’t need explicit locks. Either the whole operation fails or it succeeds, that’s why you work with a while loop, trying to execute the atomic operation as long as needed for it to succeed. In best case, the operation only needs a single try, at which point it’s much faster than always locking and unlocking specific parts of the code.

The question is more: would the some cases where this is used justify making the syntax more complicated just for saving one character token (and possibly some whitespace)?

As this is completely dependant on one’s opinion, that’s the exact reason why I’m trying to convince people through examples. That’s how low-level programming languages evolved into high-level programming languages. People repeatedly wrote code, that may as well be abbreviated through some kind of construct to simplify code writing and/or reduce the chance of producing errors.

Personally, I’d like to have this shorter syntax, but if that feature is deemed unnecessary by the majority, then so be it. 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Loops: while and for - The Modern JavaScript Tutorial
The while loop has the following syntax: while (condition) { // code ... For instance, a shorter way to write while (i !=...
Read more >
Python syntax for an empty while loop - Stack Overflow
The pass statement does nothing. It can be used when a statement is required syntactically but the program requires no action. http://www.
Read more >
do...while - JavaScript - MDN Web Docs - Mozilla
A statement that is executed at least once and is re-executed each time the condition evaluates to true. To execute multiple statements within...
Read more >
JavaScript Loops Explained: For Loop, While Loop, Do...while ...
Syntax. The while loop starts by evaluating condition . If condition evaluates to true , the code in the code block gets executed....
Read more >
Python "while" Loops (Indefinite Iteration)
pop() method and the list is empty, a is false, and the loop terminates. Remove ads. The Python break and continue Statements. In...
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