Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should we avoid plural names for variables? [closed]

Sometimes it is tempting to name a variable as plural when its a collection of objects like an array or list. Is this okay or should we always stick to singular names for variables? As one example a collection of cars could be called 'cars' or 'car'

Consider another example:

    vector< string > students; // it is named as students rather singular student

    students.push_back("Mark");
    students.push_back("Steve");

     // lets say we are using index to retrieve it, it does look 
     // a little clumsy

    string current_student = students[0]; 

Alternatively we can define the container object as singular like below but does it look like to represent a single object now instead of collection of students? It does make using index look better though.

    vector< string > student;

Another option that I personally like is something like this:

   vector< string > student_list;

I append '_list' (or could be camel notation) to the collection variable name (irrespective if its vector or list or map). This way name of the object is singular yet it identifies itself as collection of objects.

Which is a better way or convention and more readable of the above? Should plural names be absolutely avoided?

Also think of another simpler example as well, say we are conducting an experiment where we record temperatures of a day 100 times at different intervals so we :

     float temperatures[100]; // or temperature[100]? or temperature_list[100]?

or perhaps even different like:

    float temperature_data[100]?
like image 841
zar Avatar asked Oct 05 '11 15:10

zar


People also ask

Can variable name be plural?

A suggested practice is to make all variable names either singular or plural. Having two variables with names differing only by a final letter s should be avoided. An acceptable alternative for the plural is to use the suffix Array.

Should array names be plural?

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.

What are the 4 rules for variable names?

Variable names are case-sensitive (age, Age and AGE are three different variables) There is no limit on the length of the variable name. A variable name cannot contain spaces. The variable name cannot be any Go keywords.

What variable names are not allowed?

Specifically, spaces are not permitted in the variable names, as variable name must be a single word. Variable name may not start with a digit or underscore, and may not end with an underscore. Double underscores are not permitted in variable name.


2 Answers

Plurals are fine -- in fact, i'd go as far as to say expected - when the name refers to a collection of items (ie: a set, list, vector, array, etc). Names like temperature_data can be ambiguous as to whether they refer to a single item or a bunch of them. temperatures, on the other hand, clearly refers to more than one.

like image 194
cHao Avatar answered Oct 13 '22 01:10

cHao


Err, why would it not be okay? In the end, it doesn't really matter which convention you'll come up with, what matters is that it's used consistently, and doesn't impact readability negatively.

like image 21
Cat Plus Plus Avatar answered Oct 13 '22 00:10

Cat Plus Plus