C/C++: Naming conventions for arrays?
Solution 1
Name them for what they hold. An array of messages should be called messages
. An array of file names should be called filenames
. Capitalize to suit your whims and prejudices.
There are those who argue for the singular, suggesting that message[i]
is singular and better than messages[i]
. There is some justice in that, but the counter-argument is that the variable names an aggregate, so the plural is better.
I'd not use a suffix such as _list
for an array.
Solution 2
A naming convention we had at a previous job I worked at was variables started with some letter to indicate their scope (m_ for member variables, a_ for parameters, etc). Then after the underscore was another letter or two indicating the type (_p for pointers, _dw for ints (dword), _s for strings, etc) then the variable name. You could just adopt something similar to this, all that really matters is your code is readable to your convention, so you could start all of your arrays with something like a_VariableName
to say that since it starts with an a_
it is an array
Solution 3
I rarely use "primitive" containers directly. Rather, most often, the array (or vector) is a member in a "real" class that is a "first-class-citizen" that logically identifies some logical aggregate. Thus, the name of the array is an obvious data-member name for the class:
class MyCritters
{
private:
std::vector<Critter> critter_set_;
//...or...
Critter[10] critter_set_;
};
This has the added advantage that I have "intercept points" to perform logical set operations, ensure non-NULL, ensure uniqueness, add type-specific iteration algorithms, etc.
However, if I were to simply use an array directly, I typically append "set" to name. On my system, I have common interface functions for arrays, vectors, linked lists, etc., and those are all (ordered) sets. Thus, "set" will always be correct, and I can simply change the definition (instantiation), and all the other accessors still work correctly.
binW
I am a software developer from Pakistan. I like cooking, watching movies and site seeing.
Updated on June 16, 2022Comments
-
binW almost 2 years
What is a good naming convention for arrays? I am working on a code base with few thousand line of codes and there is no consistent naming convention for arrays. Few ppl name them by appending List at the end of the name like *message_list*, which I really dont like as it wrongly suggests that this is a list(linked list), and few people name them by appending 's' at the end like messages which is better than previous approach but sometimes fails.
I want to know if there are any naming conventions out there for array type variables?
-
Dan F almost 13 yearsHonestly, I got so used to it that I find code without it a little hard to read
-
Cody Gray almost 13 yearsdwords are, of course, not integers.
-
Dan F almost 13 yearsan
int
is a dword though...in most compilers -
Cody Gray almost 13 yearsNot in the Windows world.
DWORD
is always an unsigned int. -
Dan F almost 13 yearsYeah, I recall now that we did use it for unsigned ints, but we also did most of our programming in PS3
-
Thomas Matthews almost 13 years+1: "Capitalize to suite your whims and prejudices."
-
Jonathan Leffler almost 12 yearsA mathematical set has no duplicates; arrays in general can have duplicates. I'd not use
_set
as a suffix unless the array was constrained to be a mathematical set without duplicate entries. -
charley almost 12 years@Jonathan, somewhat agree, but "set" is a standardized interface where the implementation may be "vector" or "linked-list". Change type, and recompile, interface does not change. By our convention,
MyCritters
is known to be a unique collection ofCritter
instances, while we typically also haveMyCritterSet
(as the data-member withinMyCritters
) which is not enforced to be a unique collection, may contain NULL, and may actually be implemented as avector
orlinked-list
(as an implementation detail). -
crayzeewulf over 9 yearsStroustrup's opinion on Hungarian Notation.