How to name a variable: numItems or itemCount?

Viewed 11605

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;
6 Answers

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

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.

Related