hoodwink.d enhanced


C-Style For Loops Are Messed Up! #

by why in inspect

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.

said on 03 Mar 2005 at 17:16

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.

said on 03 Mar 2005 at 17:35
 i = 10;
 while (i--) ...
Is a lot nicer than a for loop IMO, but it still can’t touch 10.times.
said on 03 Mar 2005 at 19:03

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:

3.times {puts “Hello”}

and I said “I’ve gotta get me some of that!”

said on 03 Mar 2005 at 19:54

Andy Thomas being a hybrid of the Pragmatic Programmers? :)

said on 03 Mar 2005 at 21:38

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:

 c.erase(remove_if( c.begin(), c.end(), _1  3 ), c.end())

But that can’t match the elegance of:

 c.delete_if { |x| x  3 }
said on 03 Mar 2005 at 21:40

Ugh! Bad formatting! I guess the “pre” is unnecessary…

 c.erase(remove_if( c.begin(), c.end(), _1  3 ), c.end())
said on 03 Mar 2005 at 22:58

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.

said on 04 Mar 2005 at 02:37

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

private String name;
public String getName() { return name; }
public void setName(String name) { this.name = name; }

Although the Java getters can be generated easily, with the Ruby construct attributes are implicit and elegant, IMHO .

said on 04 Mar 2005 at 04:25

Hmm, but I would like the C style for to be available in Ruby too…

said on 04 Mar 2005 at 07:52


said on 04 Mar 2005 at 10:53

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.

said on 04 Mar 2005 at 11:14

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.

said on 05 Mar 2005 at 11:31

Oh yeah, one more thing.


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.


Can you guess who’s coding loops in C right now?

said on 08 Mar 2005 at 09:47

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.)

said on 05 May 2005 at 03:03

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.