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]?
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_datacan 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.