C-Style For Loops Are Messed Up! #
After coding in Ruby for four years now, it’s finally beginning to dawn on me how syntactically awful some traditional control structures are. The worst of which is the for
loop!!
I mean look at this!
for ( i = 0; i < strlen( poem ); i++ ) { printf( "%d", poem[i] ); }
I have used this structure almost daily—and I’m sure it ultimately averages out to daily—for like a decade. Which is definitely evidence of its usefulness. But the triple expression, consisting of initiator, terminator, and incrementor, is truly hideous.
What blows my mind is that, though some of C’s legacy doesn’t carry on, this structure pervades PHP, Perl, Java and other C offspring. On the other hand, I wonder how I would feel about the syntax if more control structures used this kind of multiple expression setup.
FogMustyP
I hate for loops with a passion. I’ve been a professional C/C++/ObjC programmer (primarily) for over ten years, and I hate typing out all that boilerplate 90% identical to every other for loop I’ve ever written every damn time I need a loop.
This is one of the things that Ruby brings blessed release from. I loooooooove Ruby-style iterators.
Me
10.times
.tim
This is what got me started on Ruby to begin with! I read an interview with Andy Thomas in our local paper in which he mentioned Ruby. He said that in Ruby you write a loop like this:
and I said “I’ve gotta get me some of that!”
batkins
Andy Thomas being a hybrid of the Pragmatic Programmers? :)
Bheeshmar
I came to Ruby from C++, and I hate the for-loops I have to write in Java to iterate over collections. Even C++ is better with the STL and the Boost Lambda Library:
But that can’t match the elegance of:
Bheeshmar
Ugh! Bad formatting! I guess the “pre” is unnecessary…
norseman
You know it’s an icky drudgery feature when most IDE ’s have keyboard codes to fill out the majority of the loop construct to help you.
zf
norseman, I wouldn’t say it’s about typing the darn thing (as you said, IDEs have shortcuts, like ‘for’ + ctrl+space in Eclipse), it’s about keeping your code clean and not so overly verbose.
The same thing with attribute readers/writers in Ruby vs. getters/setters in Java: in Ruby you have
attr :name
and in Java
Although the Java getters can be generated easily, with the Ruby construct attributes are implicit and elegant, IMHO .
tml
Hmm, but I would like the C style for to be available in Ruby too…
pmcm
Weirdo…
William
Using for loops is a lot like having lice. If you’re used to them, you wonder what the big fuss is. When you get rid of them, you finally understand.
Ruby was my introduction to internal iterators, and realizing just how prevalent, easy and useful they are was one of the big things that kept me interested.
Merc
That’s the first thing that hooked me on Ruby. Even before I learned the Ruby way, I had a little voice in the back of my head sigh when I had to write a loop in PHP , Java, C, Python, Perl… It was even worse when dealing with lists, where In addition to all the above, I had to figure out the size of the list.
Fixnum#times and Enumerable#each are my friends. Thanks for giving them their due.
Merc
Oh yeah, one more thing.
Grrrr….
Why the heck should I have to figure out what kind of temporary loop variable I can safely use by checking enclosing loops, when I’m only ever going to use that variable as a loop counter?!? If I use ‘i’ and then copy and paste that code elsewhere, I’m in trouble if I don’t first check to see if ‘i’ is already used as a loop there.
Grrr…
Can you guess who’s coding loops in C right now?
adrianh
Preach it brother!
(Actually, to be fair, seeing a C style for loop in Perl will almost always be somebody doing something the hard way rather than using foreach/map/while.)
dca
Anybody ever used Sather? That little beautiful (but dead) language has parallel iterators a la ruby built in. I think the iterator/yield keyword idea comes from CLU , but I never found much info on that one.
Comments are closed for this entry.