February 10, 2004

What shall we call the new for loop?

Java 1.5 introduces a new looping construct that the public review draft calls the "enhanced for loop". That name is fine for the JSR process, but I don't think it will catch on for colloquial use, and I don't think it is suitable for those of us who have to write books about the new features of the language. The problems with "enhanced for" as I see them are:

  • It inaccurately implies that this new for loop is somehow an enhancement of the original one. But it is not. It is often useful in place of the original for loop, but it is a completely different construct. It would be more accurate to call it the "new for" instead of "enhanced for". But this brings us to the second problem:
  • Both "enhanced for loop" and "new for loop" are vague names that are not descriptive. They beg the questions "enhanced how?" and "what's new?"

So, I think we need a different name. All the other statements are named after their keywords. But this one shares a keyword with the original for loop, and we can't call them both "for".

There would be no problem, of course, if the new loop had its own keyword. Then it could be a "foreach" loop or a "for/in" loop. I'm on the expert group for JSR 201, however, and despite my intense lobbying, the group consensus was strongly against introducing a new keyword (other than "enum") to the language.

We're stuck with the syntax we have:

for(Type Identifier : Expression)

This syntax has lead me to consider calling this new statement the for(:) loop to distinguish it from the for(;) loop. Get it? The loop names are distinguished by the punctuation characters they use. I threatened to use these names in Java in a Nutshell when I was trying to convince the expert group to use use a different keyword. But those names are ugly and really hard to type, and I can't go through with it.

Despite the lack of a foreach keyword, I've started to see people (myself included) use the name "foreach". This new loop is a lot like the foreach loops of other languages, and it is a nice, easy and descriptive name for the statement. The problem though, is that it makes people think that there is a foreach keyword! I think we can do better, and that we should stop calling it foreach before it is too late.

The only names I can suggest are related to the fact that this loop uses (except in the case of arrays) the java.lang.Iterable and java.util.Iterator interfaces. I think we should call it the "iterator loop", "iteration statement" or "iterative for". I favor "iterator loop", although all three of these options could be used more-or-less as synonyms.

Your suggestions, comments, and votes are welcome. I've turned on anonymous comments to make it easy to get community input on this. But that will only last for a few days and then you'll have to give your e-mail address again. (Update: anonymous comments are no longer allowed)

Here are the possible names I've mentioned, for easy reference:

  • enhanced for
  • for(:)
  • foreach
  • iterator loop
  • iteration statement
  • iterative for

Update: Thanks to those who have commented and pointed out that "iterator loop" isn't descriptive enough: all loops do iterations. And many loops use iterators explicitly. It would seem that those would be "iterator loops" too. So how about auto-iterator loop. It gets a little wordy and awkward, but it is the most descriptive and specific name so far proposed. Plus, it goes with the other automatic new features of Java 1.5: auto boxing and auto-unboxing | TrackBack

Comments

"Type 2 for", "Iteration Injection". seriously the 3 iterxxx variations are not good either, they are sementical and applicable to old for loop perfectly as well. the nick name should stress the syntactical difference. I'll say, for no good reason, "forIterable", "beauty-for"

Posted by: z at February 10, 2004 05:39 PM

The 'former' loop?

Er. or should that be the 'latter'.

How about 'ior' loop. Pronounced like the donkey noise.

Posted by: James at February 10, 2004 06:26 PM

My vote would be to used "enhanced for loop". It's the official name agreed upon by the language designers.

I'm also comfortable with calling it the "foreach loop". It what a similar feature in C# are called. And Java is immitating C# here.

The "for(:)" name conveys the syntax very clearly, so I would be comfortable with that. Except that when you read it, you have to say "for(colon)".

If you combine "for(:)" and "foreach" into "for(each) loop" or even "for (each) loop", then you can get across the idea that it is still a for loop, just a little different to warrant a parenthetical clarification.

The problem with the suggestions that does not contain the word "for": "iterator loop", "iteration statement" is that it might be a challange for the unsuspecting reader to recall the syntax. If I'm asked to write an "iterator loop", say, in a job interview, I could be really comfused. (And if I write a "do" loop, how can they say that I'm wrong---as long as I actually use an iterator?)

Similarly, is

for (Iterator iter = a.iterator(); iter.hasNext();) { System.out.println(iter.next()); }

an "iterative for"?

Posted by: Anomynous Coward at February 10, 2004 07:40 PM

foreach
sweet and simple ...

Posted by: at February 10, 2004 08:34 PM

foreach.

Posted by: at February 10, 2004 09:14 PM

How about for-each? Imply with the '-' that it's not a keyword, but use the wording people understand.

Posted by: Chris Thiessen at February 10, 2004 09:53 PM

I like foreach.

Posted by: Keith Lea at February 10, 2004 11:04 PM

You're on the expert group ? What were their concerns for not using a new keyword ? Can't we take the same approach as with the "assert" in 1.4 ?

Posted by: at February 11, 2004 12:37 AM

Apparently the addition of "assert" caused a lot of trouble for existing code and Sun took a lot of grief for it, and don't want to repeat the experience.

Obviously, new keywords cannot be added to the language casually. I think "enum" is justified, for example. And I also think that something like "anno" should have been added instead of @interface for defining new annotations. I sent this comment to the JSR-175 expert group, but they weren't interested.

Whether it is worth adding a "foreach" keyword for this new loop is less clear to me. However I do wish that we could have added "in" as a kind of pseudo-keyword that was reserved only within the parentheses following the for statement.

Alas, no such luck.

Posted by: David Flanagan at February 11, 2004 12:44 AM

'for each'
It's a 'no brainer' as those Americans would say.

Posted by: IM at February 11, 2004 02:36 AM

How about "five", it's better than "for" ;-)
[try inflate: http://www.kor.dk/borge/b-story-1.htm ]

Posted by: jez at February 11, 2004 07:03 AM

Foreach works for me. I certainly would see myself more likely to say "Use foreach" to a colleague in casual conversation than "Use the enhanced for loop".

As a random aside, Perl has it both ways. You can use either 'for' or 'foreach' with the same results. From 'man perlsyn':

"The "foreach" keyword is actually a synonym for the "for" keyword, so you can use "foreach" for readability or "for" for brevity."

Posted by: Charles Miller at February 11, 2004 02:18 PM

How about: "short for"-loop?

Posted by: grove at February 12, 2004 04:41 PM

forin loop

Posted by: at February 15, 2004 05:27 AM

"However I do wish that we could have added "in" as a kind of pseudo-keyword that was reserved only within the parentheses following the for statement"

Yes! Yes. I really wish they had done this - and it really doesn't seem like it would break almost any existing code. *sigh* Now, for once, java will have more archaic syntax in one area than C# :-(

Posted by: Will Gayther at February 16, 2004 10:43 AM

in Math... don't they call it "for all"?
it's that upside-down A that represents it:

\.........../.........--------
.\____/...........|____
...\..../.............|
.....\/........x.....--------.....S

means "for all x element of S"

Posted by: roni koren at April 14, 2004 01:35 AM