« WXS is like | Main | There's not a lot of money in revenge »

There should be one and preferably only one obvious way to do it

So, what could be simpler than looping through an array of integers in C#? Which way would you do the loop?
   1. foreach (int i in foo) {}
   2. for (int i = 0; i < foo.Length; i++) {}
   3. int len = foo.Length; for (int i = 0; i < len; i++) {}
[...excellent analysis...]

- Joshua Allen

Maybe this is stupid... but doesn't the problem go away when there is only one way?

April 22, 2004 03:19 PM


G'irts Kalnins
(April 22, 2004 04:39 PM #)

I would choose nr. 1, because it would help me to find bottleneck easier.

If the only way is much worse that one user can imagine, where is satisfaction?

I've already written one year too much.

Someclass::iterator it;
for (it = container->begin; it ++ ; it != container->end)

(April 22, 2004 05:38 PM #)

Don't forget about using a while loop.

(April 22, 2004 05:58 PM #)

#2 and #3 are more or less the same.
#1 doesn't give you access to the index the way #2 and #3 do.

Jon Hanna
(April 22, 2004 06:03 PM #)

#1 (or G'irts Kalnins' STL approach, my personal favourite) are applicable to more general cases than just arrays.

If you really need to know the index then you can just keep count. But indices are of little use with a structure like a list (unless you want to have linear time complexity).

Joshua Allen
(April 23, 2004 12:06 AM #)

"doesn't the problem go away when there is only one way"

Yes, good point. Some collections offer only one way, and I suppose the availability of foreach over arrays is considered an additional "feature". This is one reason I like lisp; the idea of a list is very elegant and consistent.

(April 23, 2004 12:12 AM #)

TOOWTDI is overlooked IMNSHO.
There *can't* be an obvious way, cause different people have different expectations.

Python pushed this concept for long time, yet Python does not respect it too.
Just think of how many ways you could concatenate strings in a list:

for line in list:


string = ''.join(list)


for line in list:

not to mention using reduce(), list comprehensions and so on. There is not just on obvious way to do it

Mike Kozlowski
(April 23, 2004 03:43 AM #)

I hope Joshua Allen is kidding with his praise of Lisp, there. I mean, you could use loop, do, dolist, dotimes, recursion with car and cdr, maybe mapcar...

At any rate, I think it's clear that C# has a preferred way for this case: Use "foreach" unless you've got a reason not to. Iterating with a C-style for is going to be possible as long as that construct is in the language (and I think it makes sense to have that in C#), but foreach is the obviously idiomatic way to go.

Tom Hawtin
(April 23, 2004 07:49 PM #)

I don't think it's unusual to use the index of a list. For instance updating a list of presentation components from a list of models. 2 and 3 only come unstuck if used on something like a large linked list (which doesn't cache index look ups). foreach is nice and clean, but not if you're duplicating the work of keeping track of the index.

Trackback Pings

TrackBack URL for this entry: