72

When I'm naming array-type variables, I often am confronted with a dilemma: Do I name my array using plural or singular?

For example, let's say I have an array of names: In PHP I would say: $names=array("Alice","Bobby","Charles");

However, then lets say I want to reference a name in this array. For Bobby, I'd say: $names[1]. However, this seams counter-intuitive. I'd rather call Bobby $name[1], because Bobby is only one name.

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

Ola Ström
  • 4,136
  • 5
  • 22
  • 41
stalepretzel
  • 15,543
  • 22
  • 76
  • 91
  • 7
    When you call $names[1], you would believe that the variable contains many names and you are referencing one instance of that array. When you think of a variable called $name, you would think it only contains one name. – Ólafur Waage Dec 28 '08 at 03:15
  • 5
    For a multidimensional array of names, would we use `$namess[1]`? – xikkub Sep 05 '13 at 15:30
  • So many opinions that don't consider what if the array only has one element? Does plural make sense then? Nope. The fact you type cast "array" everywhere is enough to let people know it's an array, with anything from zero, one, or multiple values. The plural argument is edging on Hungarian notation which is terrible. If you name variables better you don't need plural, and foreach is fine. `array $clientName` - `$clientName[0]` and `$clientName[1]` and so on sound fine, `foreach ($clientName as $name)` is also fine. In English "For each client name". No plural!! – James Jan 06 '22 at 02:08

12 Answers12

84

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

$name = $names[1];
tvanfosson
  • 524,688
  • 99
  • 697
  • 795
  • 1
    yup always plural, or mention that its a collection/list in the name – Ari Ronen Dec 28 '08 at 04:05
  • 2
    Always plural. Absolutely always. Everything else is highly misleading. – Thorsten79 Dec 28 '08 at 10:06
  • 41
    $sheep = $sheep[1]; doh! – FryGuy Dec 30 '08 at 00:15
  • 2
    I totally agree with author. But it means that we, programmers are inconsistent. Because we always intend to use standards. Collections we call plural $names however it was discussed at SO that it's better to name tables in DB in singular, for example table student. How to understand us? –  Jun 05 '15 at 15:55
  • "$name = $names[1]" what when you access name 2? `$name2 = $names[2]`?? Also, why set a new variable when you already have one (the array)? Just use the array in place you already have created? `Your 2nd user is called $name[1]`. "$names[1]" returns a single name, so doesn't the plural make it confusing? It sounds like "$names[1]" is a collection of names itself not one name – James Jan 06 '22 at 02:12
19

Name should always convey as much information as possible in case a reader is not familiar with the type declaration. An array or collection should therefore be named in the plural.

I personally find $name[1] to be misleading, since it means "the 1st element of name" which doesn't make English sense.

Uri
  • 88,451
  • 51
  • 221
  • 321
17

I usually give it something on the end like list so it would be

nameList

Otherwise, I make it plural.

James Van Boxtel
  • 2,365
  • 4
  • 22
  • 33
15

Plural.

sort(name)
sort(names)

Clearly, only plural makes sense here.

And then, here:

name[1]
names[1]

Both would make sense in this context.

Therefore, plural is the only one that makes sense when referencing the whole collection and when referencing one item from the collection.

nicholaides
  • 19,211
  • 12
  • 66
  • 82
  • What about languages with `foreach names as name`? Would not `foreach name as currentName` fit better? Also `sort(nameList)` makes much more sense. – Evgeniy Chekan Dec 24 '13 at 07:31
14

I would always go for

appleList 
appleArray
nameAppleDict

By having the naming convention done right, it will save a ton of time for someone else to read the code. Since they don't have to go back and check the variable type to understand it.

Having a variable name like:

apples 

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

Toby D
  • 1,465
  • 1
  • 19
  • 31
  • 1
    This makes more sense. It helps anybody reading the code to quickly understand what type of variable is, even yourself when you come back after long time to read your code. – Andres Ramos Nov 24 '16 at 16:16
  • 1
    This is referred to as "Hungarian Notation", and is considered bad practice for a number of reasons. – holmberd Jun 23 '18 at 15:33
2

Plural for me.

For all the reasons given above and because the agreed conventions where I work (that I contributed to creating) require plurals for arrays / lists / vectors etc.

Although plural naming can cause some anomalies in some cases the majority case is that it provides enhanced clarity and code that is easier to scan read without that annoying feeling of your mind catching on a strange construction and interrupting the flow while you go back to unsnag your brain from whatever tripped it up.

jwpfox
  • 5,124
  • 11
  • 45
  • 42
1

Plural although the teach you to do it singular in school so you can say:

value[0] = 42;

and really if you think about it that does make more sense than:

values[0] = 42

say it out loud if you don't believe me. Regardless I do use plurals so that I can easily tell when I am scanning through code. That also seems to be the standard that people are using these days.

Matt Campbell
  • 253
  • 2
  • 10
1

What the others said: plural.

It is even more flagrant in PHP:

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

will display o. :-)

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

PhiLho
  • 40,535
  • 6
  • 96
  • 134
0

I normally use the plural form, or sometimes the same way as cited up here, adding List to the name...

Vargas
  • 2,125
  • 2
  • 33
  • 53
0

I work in a lot of different languages, one thing that isn't considered is languages which have more than array. i.e person:Person; people:Dictionary. people is not necessarily an array, it could be of another type and cause an error. Also, in some languages the different types will perform better at different operations or possibly have different methods available to them.

Which is why these days in all languages I make the names with the noun singular followed by the type such as personArray or person_arr if you prefer. I generally also include the scoping at the beginning if relevant. Variable names should be explicit enough that you don't need auto complete or ctrl+f to know what it is.

Cubed
  • 1
0

Always plural. Same for lists of any other datatype that can hold more than one element.

Robert Venables
  • 5,943
  • 1
  • 23
  • 35
0

Always plural. That way there isn't any confusion when I do...

for each (string person in people)
{
    //code
}
William Holroyd
  • 3,344
  • 1
  • 21
  • 25
  • I tend to use $peoples or $childrens. Even though it's not correct english, you can see what it means. That way, I am using a convention that plural is the singular with an 's' appended. Though I am still not sure my convention is the best. – Rimian Jul 01 '10 at 00:18
  • @Riman. `$childrens` is a double plural (so is *children*, etymologically, as both *-er* and *-en* are plural suffixes, and *childer* can still be heard in some parts of Northern England). And a people is a group of persons. – TRiG Apr 04 '12 at 00:04
  • 4
    That's why I always use `$peopleses` and `$childrenses`, just to be sure. – kraxor Dec 09 '13 at 02:24