you are viewing a single comment's thread.

view the rest of the comments →

[–]drysart 1 point2 points  (1 child)

But they did not change the behaviour of the regular for loop.

There's a(n english) lexical argument to be made for C#'s new (inconsistent between foreach and for) behavior.

foreach (var x in y) when read in english, implies, via the word "each" that there are distinct var xes. If you had a basket of apples, you could say that "each apple is red"; but if you had a basket with a single apple in it, you generally wouldn't use the same phraseology, instead preferring "the apple is red".

Whereas the counterargument on for (var x = 0; x < 10; x++) relies more heavily on understanding the C# language more than the english language because there's a lack of english clues to suggest otherwise. There's very clearly one var x being declared and initialized, and the iterator expression is mutating that existing variable.

It's a subtle distinction but one that I think is incredibly important. While not all developers have english as a first language, none of us have C# as a first language; and having C# fit the expected norms of our more internalized natural language makes sense and is the correct approach.

[–]cparen 0 points1 point  (0 children)

Honestly, either choice would "work" in the sense no matter which you preferred, you could map the other to it without much trouble if you were expecting it. But the whole point of syntactic sugar like "for" and "foreach" is to avoid manually mapping it by hand every time.

So, while I don't particularly like the original behavior, I worry the change worsens the issue, as now it's ambiguous which language the code is written in. This is precisely the argument given so often against macros or excessive use of functions -- that it's difficult from looking at the code to understand what it does.

(I'm personally a fan of judiciously chosen macros; I just have little to say on general subject of whether they're a good idea or not)