Index Or Count

The first reference I ever saw to this issue on the Wiki was in IdentifiersAreComments by DaveHarris.

You've surely seen code like:

        int number_of_elements;

I think this means "the count of elements". Then there's:

        int number_of_selected_element;

which I think means "the number that identifies the selected element". The subtlety is confusing, which is enough reason to not use "number" in a name.

But it gets worse:

        int num_elem;

which is it?

The solution: use "count" or "index" instead of "number". If you like prefixes, 'c' and 'i' work well.

For even worse naming:

        int no_elem;

does this mean "count", "index", or "yes/no"?

-- JayBazuzi

I suggest "elements" or "elts" for number_of_elements, and "element_idx" or "elt_i" for number_of_selected_element. -- PanuKalliokoski

"elts"? T unse it rung ot f chrs. Ths n nd t cove tm. Pe he se my o yr auce ad ge tm cote ws.

"elts"? The universe isn't running out of characters. There's no need to conserve them. Please have some mercy on your audience and give them complete words.


I use count or number_of for the total amount, and _index for a specific element. Using number to denote which of a set seems unwieldy to me. Typically, however, I'm using this in a loop, so I just use the loop variable to identify the current element and if I need to save it off for some reason (comparison, etc), I have a new variable of the index type and call it biggest, or marker' or some such. --PeteHardie


EditText of this page (last edited December 16, 2003) or FindPage with title or text search