I have seen numerous developers postfixing variable names with a "List" when it's an array. What's the point of this and would you encourage th开发者_如何学运维is style? For example:
// Java
String[] fileList;
String file;
// PHP
$fileList = array();
$file = '';
The idea applies to any language with support for arrays.
The idea? Readability - you can tell the variable is a collection in one glance. This can be achieved with pluralizing the variable name (ie. files
).
If the fact that the data type is a list is significant (assuming several different collection types), then using that postfix is the right thing to do (as opposed to simply being a collection).
I personally tend to pluralize variable names, using a list
postfix only if it adds information or can't be inferred from the name otherwise (say a list of lists).
I think this is mostly a matter of taste. I tend to name things to reflect what they are or what they do. So if I have a collection or array of File
instances, the variable would most probably named files
, or have a more specific name if the context allows it. Naming an array of Files fileList
is in my humble opinion plain wrong, because, at least in Java, an array is not a List
. But then, the compiler won't complain...
More complex collections like a Map
get names like keyToValue
. So if I had a map which assigns teachers to classrooms this would be called teacherToRoom
in my code. I hate grepping through the code to find out what the variables are meant to do, so I try to be as specific as needed with the names.
In conclusion it's all about correct code, and variable names can not influence this outcome from the compiler perspective. But they can very well affect the outcome when it comes to humans working with the code, so it's best to do whatever works for the majority of people working on a codebase.
精彩评论