Array naming convention
Author |
Message |
Insectoid
|
Posted: Fri Jan 21, 2011 7:45 pm Post subject: Array naming convention |
|
|
I'm just wondering if there's a convention on the naming of arrays regarding pluralization. Say I've got an array of card objects, would it be more appropriate to call it cards[] or card[]? I suppose I'll provide some reasoning for this question-
Calling the array card[] allows you to read calls of the array more like English- card[5] sounds a lot like 'card 5'. Similarly, cards[5] sounds like a 2d array if cards- 'set of cards 5'. This of course is remedied by calling the array 'deck' but there are situations where words like 'deck' are unavailable (array of unrelated cats?).
There is of course the argument that cards[] better shows that the array is indeed a group of cards instead of an array of information on one card.
And then you can just call it card_array[]/cardArray[]/whatever convention you use for multiple words in a variable name.
I find I use each method on a program-by-program basis, but I'm fairly consistent within programs themselves.
Thoughts? |
|
|
|
|
|
Sponsor Sponsor
|
|
|
RandomLetters
|
Posted: Fri Jan 21, 2011 9:24 pm Post subject: RE:Array naming convention |
|
|
I prefer to use cards[] since in java arrays comes with their own functions, and are used without the []. This makes it more obvious that it is an array of cards when doing things such as cards.length. |
|
|
|
|
|
DemonWasp
|
Posted: Fri Jan 21, 2011 11:02 pm Post subject: RE:Array naming convention |
|
|
I prefer using the plural form for collections (array, list, map, set). This lets you then use the singular form for an entry in the set very simply. I'm a fan of variable names that describe their contents. |
|
|
|
|
|
yoursecretninja
|
Posted: Sat Jan 22, 2011 12:23 am Post subject: RE:Array naming convention |
|
|
I recommend using the plural form as a naming convention for arrays. At work, I insist on it This is why.
REASON 1 (most important): It makes sense.
Consider this really simple example in PHP.
$number = array(0, 1, 2, 3, 4, 5);
That's just weird. If I saw this variable in code but didn't see its declaration, I (and probably most other people) would assume that number was a single number and not a collection of numbers. I'd probably use this inappropriately resulting in an error and wasted time debugging.
Say what you mean.
$numbers = array(0, 1, 2, 3, 4, 5);
Now that makes sense. If I saw the $numbers variable in code I would intuitively know that this was a collection of numbers and use it appropriately. Frustration avoided.
REASON 2: It makes iteration make more sense
A common way to print out the contents of an array like $numbers is to use a for loop.
for ($i = 0; $i < count($numbers); $i++) {
echo $numbers[$i];
}
Sure, every programmer knows what this does. But it's ugly and not semantic. Fortunately, most languages provide a shorthand method for iteration, often called foreach. So you could also iterate through and print out the contents of $numbers like this:
foreach ($numbers as $number) {
echo $number;
}
That makes a lot of sense - what's happening here is practically written in english. As an added benefit of using such a construct, you don't need to know that a collection starts at index 0 and ends at index n.
Now if you had declared $number = array(0, 1, 2, 3, 4, 5) you would have to use the above construct in a silly way defeating much of its benefit. For example:
foreach ($number as $num) {
echo $num;
}
From a semantical perspective, that's just nonsense.
MORAL: Just like in english, if you are talking about a collection of objects, use the plural form. It makes things easier for you and those that have to read your code. This is so important when you are out in the work world or working with others on the project because no one wants to work with a programmer if their code doesn't make sense without having to constantly cross reference it.
There's my rant for the evening |
|
|
|
|
|
mirhagk
|
Posted: Thu Jan 27, 2011 6:45 pm Post subject: RE:Array naming convention |
|
|
Here's my theory, I often use classes for related information, so I name the class the singular name, and the array the plural name. such as
class Cat
{
}
Cat[] cats; |
|
|
|
|
|
|
|