Tue 22 May 2007
The only loop, the infinite loop
Posted by Dennis B under Programming
[4] Comments
The next revision of the Slick-C language is introducing a new infinite loop statement. It is a concept borrowed from Modula and Wirth’s Oberon, and probably several other languages.
loop {
// statements
}
The idea is that you only really need one loop statement, because it’s just a matter of using if and break in the right spots. It is completely identical to the common C/C++ idiom below, just a little more obvious to the novice coder.
for (;;) {
// statements
}
For example, a while loop is just:
loop {
if ( ! condition ) break;
// statements
}
A do-while is simply:
loop {
// statements
if ( ! condition ) break;
}
A C-style for loop is more complex to code, but one could argue, quite a bit easier for the uninitiated programmer to read and understand, because you don’t have to know anything about the semantics of a for statement to see when the condition is checked and when the increment happens. It does get a bit messy if you have a continue, since you have to remember to increment before you continue.
[initializer]
loop {
if ( ! condition ) break;
// statements
[increment]
}
The generic infinite loop isn’t a convenient replacement for the foreach constructs found in some modern languages, and in fact, loop is not a great replacement for the counter-based for loops found in older languages such as Pascal and Cobol.
While Slick-C can never abandon traditional looping constructs entirely, this does give you a glimpse of what a truly bare-bones language could do. In fact, such a language could go one step further, and introduce a once block. This would allow break statements, but no continue statements. Some of you will recognize this as the equivelent to the do-while-false idiom.
once {
// statements
}
Conclusion
These constructs, loop and once are no advancements in the state of the art, they are just alternate, simpler ways to write code. Novice coders will welcome the simplicity. Experienced programmers will shrug it off as just another way to do the same thing they have been doing for decades, and are likely to even complain that it introduces more complexity since it is yet another new statement type. What loop does accomplish is making good on the promise of making code easy to write, read, and understand.
May 22nd, 2007 at 3:05 pm
You’re correct. Experienced programmers will shrug it off, or complain.
I don’t like to advocate conformity, but I do advocate consistency, where it makes sense. For better or worse, the C-style languages benefit to some degree by a consistent set of control structures.
Most programmers know how to use if(), for(), while() and do. They can apply the knowledge from other languages to Slick-C. The rare programmer who learns Slick-C before another C-style language can apply
what he learns in Slick-C to those other languages.
Of course, I don’t advocate dropping “parse with”, or other Slick-C peculiarities that are actually useful. But this new loop construct, what is it really useful for?
What’s so terrible about for(;;)???
Regards
John Hurst
June 18th, 2007 at 8:25 am
I think the loop construct is broken for two reasons:
1. While it provides more freedom to the programmer to construct their loop, it imposes no semantic meaning (other than iteration) which is likely to lead to greater inter-programmer variability and hence less readable code. Further, the existing for, while etc. constructs have lead to idiom with which programmers are familiar; loop will have to grow its own idioms.
2. Looping is just iteration over some set of items. These items may be entirely abstract (e.g. in the case of an infinite loop and other constructs). I think it is much better to use an iterator pattern where one simply iterates by getting the next item, or exists the loop if no next item exists. For me at least, this approach leads to more readable code and fewer bugs.
June 18th, 2007 at 9:23 am
First, I acknowledged in the article that loop is no replacement for foreach.
However, I strongly disagree about your second statement. foreach is one looping pattern, but generally, you loop until a condition is satisfied and then you break out of the loop. With a set of items, the condition is running out of items. But it is a mistake to shoe-horn all looping into the “iteration over some set of items” pattern. That is replacing simplicity with unneccessary complexity, and that always leads to less readable code and more bugs.
March 18th, 2008 at 10:17 am
all detail of infine looping of c language