40

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;
Pavel Alexeev
  • 6,026
  • 4
  • 43
  • 51
  • 1
    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 - on strike Jun 15 '11 at 14:05
  • @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 - on strike 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
  • 1
    Possible duplicate of [Naming conventions for "number of foos" variables](https://stackoverflow.com/questions/3742650/naming-conventions-for-number-of-foos-variables) – Oleg Estekhin Jul 25 '18 at 09:51
  • See also https://stackoverflow.com/questions/14731025/api-c-sharp-method-naming-convention-getpagecount-or-gettotalpages/14731102#14731102 – Michael Freidgeim Jun 16 '21 at 13:54

6 Answers6

47

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."

Andy Thomas
  • 84,978
  • 11
  • 107
  • 151
  • 1
    In volume 2, the quote is: > But, because using Num so often creates confusion, it’s probably best to side-step the whole issue by using Count or Total to refer to a total number of customers and Index to refer to a specific customer. – Maxime Chéramy May 27 '21 at 18:15
  • `Num` is indeed ambiguous but `numberOf` isn't. But `numberOf` is too long compared to a simple `count` – Maxime Chéramy May 27 '21 at 18:16
7

item_count or itemCount (there's a religious war brewing there, though)

William Pursell
  • 204,365
  • 48
  • 270
  • 300
4

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.

Miki
  • 7,052
  • 2
  • 29
  • 39
3

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.

Flash
  • 15,945
  • 13
  • 70
  • 98
1

It actually depends on you. The two types of naming conventions are camelCase and snake_case

As you can identify from the naming, camel case has one small letter in the initial part of the variable followed by the Capital words

Eg itemCount.

snake case is a continuous word with an underscore ' _ ' in between the words Eg item_count

As far as the naming is concerned, numItems is quite confusing for others to read. But itemCount is a good name for a counter variable

1

I've been wondering about this question too, and thought it interesting in all these answers that no one said just items, but I can see that would be a bad name perhaps if it's in a codebase that has objects or arrays, but maybe okay as like a field name in SQL.

But one downside I just realized to going with something like numItems is that if you have multiple similar fields and use anything with intellisense or autocomplete, there's a risk of accidentally using the wrong field, whereas item_count begins with the thing you're counting.

redOctober13
  • 3,662
  • 6
  • 34
  • 61