How to name a variable: numItems or itemCount?

Multi tool use


How to name a variable: numItems or itemCount?
What is the right way to name a variable
int numItems;
vs.
int itemCount;
or constant:
public static final int MAX_NUM_ITEMS = 64;
vs.
public static final int MAX_ITEM_COUNT = 64;
@Cody Gray, I agree with that general rule -- but there is a difference in this case between "num" and "count". See below.
– Andy Thomas
Jun 15 '11 at 14:19
@Andy: I mean, sure. I even agree with you and Steve. But come on, that hardly justifies this question...
– Cody Gray♦
Jun 15 '11 at 14:28
Sorry to come here late but this question should be closed. It is opinion-based
– Carlos Muñoz
Feb 18 '15 at 16:19
Possible duplicate of Naming conventions for "number of foos" variables
– Oleg Estekhin
2 days ago
4 Answers
4
In "Code Complete," Steve McConnell notes that "Number" is ambiguous. It could be a count, or an index, or some other number.
"But, because using Number so often
creates confusion, it's probably best
to sidestep the whole issue by using
Count to refer to a total number of sales and Index to refer to a
specific sale."
item_count or itemCount (there's a religious war brewing there, though)
For Java I would use itemCount
and MAX_ITEM_COUNT
. For Ruby, item_count
and MAX_ITEM_COUNT
. I tend not to use names that can be interpreted wrongly (numItems
may be a shortcut for numerate_items
or number_of_items
), hence my choice. Whatever you decide, use it constantly.
itemCount
MAX_ITEM_COUNT
item_count
MAX_ITEM_COUNT
numItems
numerate_items
number_of_items
I've never seen
numerate
used in this context. Maybe it's just me...– Zero3
Dec 2 '16 at 13:47
numerate
It's a matter of personal preference, just make sure you are consistent throughout your code. If you're working with others check what's been done in existing code.
For the constant I would find MAX_ITEMS
more logical than MAX_NUM_ITEMS
or similar, it just sounds better to me.
MAX_ITEMS
MAX_NUM_ITEMS
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Any time you're about to ask a question about the "right" way to do something, stop and consider whether it's merely a subjective, aesthetic preference. This certainly qualifies. It absolutely doesn't matter. Pick one that you like and stick with it.
– Cody Gray♦
Jun 15 '11 at 14:05