Do you name your arrays using plural or singular in PHP?
PhpArraysNaming ConventionsPhp Problem Overview
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?
Php Solutions
Solution 1 - Php
I use the plural form. Then I can do something like:
$name = $names[1];
Solution 2 - Php
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.
Solution 3 - Php
I usually give it something on the end like list so it would be
nameList
Otherwise, I make it plural.
Solution 4 - Php
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.
Solution 5 - Php
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?)
Solution 6 - Php
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.
Solution 7 - Php
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.
Solution 8 - Php
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...
Solution 9 - Php
Always plural. Same for lists of any other datatype that can hold more than one element.
Solution 10 - Php
Always plural. That way there isn't any confusion when I do...
for each (string person in people)
{
//code
}
Solution 11 - Php
I normally use the plural form, or sometimes the same way as cited up here, adding List to the name...
Solution 12 - Php
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.