dao
dao copied to clipboard
Thoughts on do-while
Just as with #388, I tread on the holy land of C-like languages, daring to cast my vicious sight on the innocent do-while loop.
Really, it's so infrequently useful, that it may be safely removed from the language together with the do keyword. As an alternative, I'd rather welcome something like loop or forever for unconditional looping which has arguably wider application. Besides, it covers the do-while scenario well enough:
do {
...
}
while (condition)
#vs
loop {
...
if (condition) break
}
Actually, you seem to be right with the do-while loop - the advantage of defining local variables is gone (compared to while loop).
But I'm reluctant to adding a special word for infinite loop - for(;;) or while(true) are well readable and both can be clearly detected and efficiently compiled.
As an alternative, I'd rather welcome something like loop or forever for unconditional looping which has arguably wider application. Besides, it covers the do-while scenario well enough:
The main purpose of do-while is the possibility of executing the loop body at least once regardless the condition. For this, loop-break is obviously less convenient.
It's just that I've noticed tendency to favor unconditional loops over do-while in some modern languages (Ruby, Go, Rust) which is mostly likely backed by the same logic -- unconditional loops are a more frequent pattern.
do-while is, of course, more convenient for its specific case, but the difference is small. And do-while loops are relatively rare; from my personal experience with C (the language I use most), do-while loops are below 1:50 ratio comparing to other kinds of loops.
Overall it is still a matter of taste, so I won't insist.
Actually, a fitting syntax for unconditional loops may be for (...) -- it's shorter and clearer then while (true), while not as alien-like as for (;;). This doesn't change my opinion on do-while, which I still consider nearly useless.
I can't say I see a visual difference between for(;;) and for(...). Both are clear, the letter though disables the possibility to declare local variables and thus is less convenient.
for (;;) is a hack; I, for one, don't like how it reads and don't use it. for (...) is a dedicated syntax which bears meaningful semantics.
@Night-walker I'm still not convinced by your inner feelings about the for (;;) syntax :smile: . On the other hand, I see any other additional syntax for an infinite loop as something inconsistent, redundant and lacking features.
I see any other additional syntax for an infinite loop as something inconsistent, redundant and lacking features
Comparing to Lisp, the syntax of most other programming languages is inconsistent, redundant and lacking features.
Stay on the floor, don't generalize and explain your feelings a little bit deeper and you might get some plus points for for (...) from me, but I don't guarantee anything, rather the opposite ;)
It would be way too boring if you just agreed :)
do-while is redundant, I just too boringly agree ;) as I wrote earlier. But introducing another additional redundant syntax sugar for an infinite loop, which is even longer to type (by 1 character, but still it is) than it's counter-part while lacking the feature of defining a local variable and doesn't even improve the readability, which is already very good and also well-known to all programmers from the imperative world (pretty much everyone) is simply not an option for me.
Of course, if @daokoder will agree, then why not to implement it - I myself though won't use it, it's that simple. Howk.
lacking the feature of defining a local variable
It doesn't meant to.
doesn't even improve the readability
Well, it does improve readability for me :)
well-known to all programmers from the imperative world (pretty much everyone)
Many languages don't have C-like loop.
Well, it does improve readability for me :)
Unfortunately not for me. For me, for(...) reads more like looping with undefined number of cycles.
Regarding do-while, I more or less agree we can remove it. But unlikely I will introduce something new for infinite loops. I think infinite loops are not very frequent, for infrequent use of infinite loops, while(true) is good enough. And I don't consider for(;;) as a specific syntax, instead, it is the natural result when the expressions are omitted. So nothing really need to be done about it.
Regarding do-while, I more or less agree we can remove it.
That's enough for me.