Do you call your arrays multiple or singular?

When I name variables like an array, I often come across a dilemma: Do I call my array multiple or singular?

For example, let's say I have an array of names: in PHP, I would say: $names=array("Alice","Bobby","Charles"); However, let's say I want to refer to the name in this array. For Bobby, I would say: $names[1] . However, these seams are counter-intuitive. I would rather call Bobby $name[1] , because Bobby is only one name.

So you can see a slight discrepancy. Are there conventions for naming arrays?

+53
naming-conventions
Dec 28 '08 at 3:10
source share
12 answers

I use plural form. Then I can do something like:

 $name = $names[1]; 
+72
Dec 28 '08 at 3:12
source share

The name should always convey as much information as possible if the reader is not familiar with the type declaration. Therefore, an array or collection must be named in the plural.

I personally consider $ name [1] to be erroneous, as it means "1st element of the name", which does not make English sense.

+14
Dec 28 '08 at 5:25
source share

I usually give it something at the end, like list , so it will be

 nameList 

Otherwise, I do it in the plural.

+11
Dec 28 '08 at 3:54
source share

Plural.

 sort(name) sort(names) 

Clearly, only the plural makes sense here.

And then, here:

 name[1] names[1] 

Both will make sense in this context.

Therefore, the plural is the only one that makes sense when referring to the whole collection and when referring to one element from the collection.

+11
Dec 28 '08 at 4:28
source share

I would always go for

 appleList appleArray nameAppleDict 

If the naming convention is done correctly, it will save a ton of time for someone else to read the code. Because they do not have to return and check the type of the variable in order to understand this.

The presence of the variable name:

 apples 

can sometimes be confusing (list, array or set?)

+10
Oct 05 '15 at 6:46
source share

Plural for me.

For all the reasons given above, and because the agreed conventions in which I work (which I contributed to the creation) require the plural for arrays / lists / vectors, etc.

Although multiple naming can lead to some anomalies, in most cases most of the cases are that it provides improved clarity and code that is easier to scan without this annoying feeling of your mind catching the strange design and interrupting the flow when you return to get rid from your brain from what worked.

+2
Dec 28 '08 at 5:16
source share

What others have said: plural.

This is even more egregious in PHP:

 $name = 'Bobby'; echo $name[1]; 

will display o .: -)

I must admit that I asked myself the same question a few years ago, but showing that the multiple nature of the array or collection was more important than English when accessing one member ...

+1
Dec 28 '08 at 9:55
source share

Always plural. The same is true for lists of any other data type that may contain more than one item.

0
Dec 28 '08 at 3:14
source share

A few, although to teach you to do this especially at school, so that you can say:

 value[0] = 42; 

and indeed, if you think about it, that makes more sense than:

 values[0] = 42 

say it out loud if you don't believe me. Regardless of the fact that I use plurals, so that I can easily tell when I look at the code. It also seems like the standard people use these days.

0
Dec 28 '08 at 4:08
source share

Always plural. So I'm not embarrassed ...

 for each (string person in people) { //code } 
0
Dec 28 '08 at 5:35
source share

I usually use a plural form, or sometimes the same as quoted here, adding a list to the name ...

0
Jul 17 '09 at 19:40
source share

I work in different languages, one thing that is not covered is languages ​​that have more than an array. ie face: face; people: Dictionary. people are not necessarily an array; it may be of a different type and cause an error. In addition, in some languages, different types will work better with different operations, or perhaps have different methods available to them.

That is why these days in all languages ​​I am making names with an exclusive noun, followed by a type such as personArray or person_arr if you prefer. In general, I also include a review at the beginning, if necessary. Variable names should be clear enough so you don't need auto-completion or ctrl + f to know what it is.

0
Jun 11 '15 at 0:37
source share



All Articles