[ACCEPTED]-Do you name your arrays using plural or singular in PHP?-naming-conventions
I use the plural form. Then I can do something 1 like:
$name = $names[1];
Name should always convey as much information 6 as possible in case a reader is not familiar 5 with the type declaration. An array or collection 4 should therefore be named in the plural.
I 3 personally find $name[1] to be misleading, since 2 it means "the 1st element of name" which 1 doesn't make English sense.
I usually give it something on the end like 1 list so it would be
nameList
Otherwise, I make it plural.
Plural.
sort(name)
sort(names)
Clearly, only plural makes sense 5 here.
And then, here:
name[1]
names[1]
Both would make sense 4 in this context.
Therefore, plural is the 3 only one that makes sense when referencing 2 the whole collection and when referencing one 1 item from the collection.
I would always go for
appleList
appleArray
nameAppleDict
By having the naming 6 convention done right, it will save a ton 5 of time for someone else to read the code. Since 4 they don't have to go back and check the 3 variable type to understand it.
Having a 2 variable name like:
apples
could be confusing sometimes 1 (list, array or set?)
Plural for me.
For all the reasons given 11 above and because the agreed conventions 10 where I work (that I contributed to creating) require 9 plurals for arrays / lists / vectors etc.
Although 8 plural naming can cause some anomalies in 7 some cases the majority case is that it 6 provides enhanced clarity and code that 5 is easier to scan read without that annoying 4 feeling of your mind catching on a strange 3 construction and interrupting the flow while 2 you go back to unsnag your brain from whatever 1 tripped it up.
Plural although the teach you to do it singular 7 in school so you can say:
value[0] = 42;
and really if you 6 think about it that does make more sense 5 than:
values[0] = 42
say it out loud if you don't believe 4 me. Regardless I do use plurals so that 3 I can easily tell when I am scanning through 2 code. That also seems to be the standard 1 that people are using these days.
What the others said: plural.
It is even 6 more flagrant in PHP:
$name = 'Bobby';
echo $name[1];
will display o. :-)
I 5 must admit I asked myself the same question 4 some years ago, but showing the plural nature 3 of the array or collection was more important 2 than English meaning when accessing one 1 member...
Always plural. Same for lists of any other 1 datatype that can hold more than one element.
Always plural. That way there isn't any 1 confusion when I do...
for each (string person in people)
{
//code
}
I normally use the plural form, or sometimes 2 the same way as cited up here, adding List 1 to the name...
I work in a lot of different languages, one 15 thing that isn't considered is languages 14 which have more than array. i.e person:Person; people:Dictionary. people 13 is not necessarily an array, it could be 12 of another type and cause an error. Also, in 11 some languages the different types will 10 perform better at different operations or 9 possibly have different methods available 8 to them.
Which is why these days in all 7 languages I make the names with the noun 6 singular followed by the type such as personArray 5 or person_arr if you prefer. I generally 4 also include the scoping at the beginning 3 if relevant. Variable names should be explicit 2 enough that you don't need auto complete 1 or ctrl+f to know what it is.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.