views:

241

answers:

8

What should I call a variable instantiated with some type of array?

Is it okay to simply use a pluralised form of the type being held?

IList<Person> people = new List<Person>();

or should I append something like 'List' to the name?

IList<Person> personList = new List<Person>();

Also, is it generally acceptable to have loops like this?

foreach(string item in items)
{ 
    //Do something 
}
+10  A: 

This is simple code convention, so you should go with what your team is used to. If you are single per project, just keep using the same convention whatever that is.

LE: the loop is ok, though it's a distinct question.

thelost
+1  A: 

Personally I'd go for "people", however this is highly subjective and not "coding standard". Go with what you feel is most intuitive.

And yes your loop is perfectly acceptable

Herter
A: 

I personally feel that a variable name should describe the type as well as the context. I personally like the personList more :) With all these 'personally' statements, I guess it's just my personal feeling :)

PieterG
+1  A: 

In the teams I've worked with, we commonly consider it good practice to not differentiate a list/array variable from a single object variable, just by using the plural form of the name.

I.e. using

object horse = new object();

and

List<object> horses = new List<object>;

will be really hard when you read the code. So you should go for something like

object horse = new object();

and

List<object> PackOfHorses = new List<Object>();

as you already mentioned.

Your loop is perfectly acceptable. Nothing wrong with that.

EDIT: Changed the phrasing and variable names due to comments.

Nikos Steiakakis
As you're the only person who has claimed "best practice" rather than "this is just my opinion," I get to disagree with you. Citation needed on your first sentence, because that doesn't look like a best practice at all. Not even the [original version of Hungarian notation](http://www.joelonsoftware.com/articles/Wrong.html) called for the data type to be part of the variable name. In addition, since when is PascalCase a best practice for variable names?
Joel Mueller
Perhaps my phrasing wasn't all that correct. By "Generally it is considered best practice" I actually meant that generally it's considered 'good'practice to not just use the plurar of a word ( horse, horses) since it is harder to observe when reading the code. I didn't provide citation, because I have not read/heard in a single place. It's just experience. As for the data type, I did not say he should use it, I just used var names simillar to the OP's question (he used personsList). In the example with the horse I just mentioned, I would go for horse - packOfHorses instead of horse-horses
Nikos Steiakakis
I've never before heard anyone say that using the plural for a list makes code harder to read.
John Saunders
@John Saunders So you say that it is just as easy for the eye to notice a single letter difference between two variable names? I'm saying that as far as readability is concerned, not in terms of undrestanding what it means. Especially when both the single item variable and list/collection variable are used within the same code listing.
Nikos Steiakakis
@Nikos: I can speak only as a native English speaker - when variables are named after real things, I tend to read the code as prose. In that case, I find it natural to notice the difference between singular and plural.
John Saunders
+1  A: 

Ideally, you would use the plural, although there are times you might want to use the other form. There are only guidelines, not hard and fast rules.

Mystere Man
+2  A: 

As mentioned before this is really very subjective but, I'd go for what looks best in the intellisense dropdown when you use the classes as that is probably the first point of interaction other developers using your code will have.

Johan Badenhorst
A: 

I don't like using PersonList since it sounds more like a class you've written to contain persons than a variable.

Matt_JD
A: 

I guess back in old days when there were no fully featured IDEs, using names like peopleList were more fitting since they could figure out the type of the variable by its name. But nowadays, that's not an issue anymore.

David Weng