tags:

views:

253

answers:

3

In a coding standards document, I found this statement:

Avoid using foreach to iterate over immutable value-type collections. E.g. String arrays.

Why should this be avoided ?

+13  A: 

You shouldn't avoid it. The coding standard document you're reading is talking nonsense. Try to find the author and ask him to explain.

Aside from anything else, string is a reference type and arrays are always mutable... this makes me concerned about the quality of the rest of the document, to be honest. Are there any other suspicious recommendations?

(It's possible that "immutable" was meant to refer to the value type rather than the collection - the fact that it's ambiguous is another worrying sign, IMO.)

Jon Skeet
And not only that but the only good value type is an immutable value type!
Daniel Earwicker
A coding standards document talking nonsense? *Stop the presses*
T.J. Crowder
This is the document in question: http://weblogs.asp.net/lhunt/pages/CSharp-Coding-Standards-document.aspx (page 16, Section 4.3, #35). I could not make sense of it either. I'll try to see if the author will comment on this.
driis
If Jon Skeet says so then it is nonsense !!!
abmv
Maybe it was a typo and they meant mutable? Enumerating over an array of strings as it is mutated really *is* a bad idea.
Strilanc
I've noticed a disconcerting **inverse correlation** between an organization's dependence on *"coding standards"* and the **quality of code** produced in that organization.
LBushkin
@LBushkin: I wouldn't say that. At Google we have a reasonably rigid coding standard, not because other ways of doing things are "bad" but because we need to be able to understand other people's code a lot. I regularly look at code from the rest of the Google code base - if every team had its own conventions, that would be a lot harder.
Jon Skeet
Jon, I'm not suggesting that coding practices are bad - on the contrary, practices that help lead to consistency and comprehensibility are wonderful. However, I have observed that many times coding practices are used as a crutch in groups that have not necessarily invested the time to learn the platform and tool set - or as a hammer by managers to achieve some abstract sense of conformance to a standard. I'll also say that I've read enough standards documents that propose meaningless or contradictory practices that it has made me (personally) a bit jaded on the subject.
LBushkin
+3  A: 

I agree with Jon that the advice makes little sense. My guess is that the author discovered that you cannot change the value of the current item when iterating. However, if you're iterating a collection of reference types you can still modify the object the current item points to. Perhaps (s)he concluded that iteration was somehow broken for value type collections.

Brian Rasmussen
+4  A: 

I think that the reason for that statement is that it's written prior to .NET 2.0.

When using foreach in .NET 1.x it was using the IEnumerable interface (as the IEnumerable<T> interface didn't exist yet.) When iterating over a value type collection, the enumerator would box each item to be able to return it as an object reference, then the foreach code had to unbox it.

A string array is of course not an example of an array of value types. An integer array is.

Guffa
Even an integer array wouldn't make sense though, as the C# compiler treats arrays differently when you use foreach.
Jon Skeet
@Jon: Yes, a foreach over an array is highly optimised in the current implementation, but it might not have been in the earliest versions.
Guffa
@Guffa: I think it's always been optimised - in fact, it makes more sense for it to have been optimised *before* generics than *after*, to avoid boxing.
Jon Skeet