【发布时间】:2011-10-05 15:36:33
【问题描述】:
当变量是数组或列表等对象的集合时,有时将其命名为复数是很诱人的。这可以吗,还是我们应该始终坚持变量的单数名称?例如,可以将一组汽车称为“汽车”或“汽车”
考虑另一个例子:
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];
或者,我们可以将容器对象定义为如下所示的单数,但它现在看起来像是表示单个对象而不是学生集合吗?不过,它确实使使用索引看起来更好。
vector< string > student;
我个人喜欢的另一个选项是这样的:
vector< string > student_list;
我将“_list”(或者可能是骆驼符号)附加到集合变量名称(无论它的向量、列表还是映射)。这样,对象的名称是单数的,但它将自己标识为对象的集合。
以上哪种方式或约定更好且更具可读性?是否应该绝对避免使用复数名称?
还想另一个更简单的例子,假设我们正在进行一项实验,我们以不同的时间间隔记录一天的温度 100 次,因此我们:
float temperatures[100]; // or temperature[100]? or temperature_list[100]?
或者甚至可能不同:
float temperature_data[100]?
【问题讨论】:
标签: c++ stl naming-conventions