Naming conventions for "number of foos" variables [closed]

I go for fooCount because it is straightforward, and I think the word "count" is the shortest and the best that describes it, not "number of" or the like.

I go for FOO_COUNT if it you need to store it final and static(if you don't need to change it/if it is a constant). (all caps for constants!)

I go for count and calling it by Foo.count if you really have to store it as an attribute for a class that you made, which is Foo.

readability for you and for your team!


Since the variable stores the count of the number of foo objects, fooCount gets my vote.


In English, the words 'number' and 'count' can both act as nouns or verbs, but it's probably more common to see 'number' used as a noun, and 'count' as a verb. So you could argue that 'the number of foos' or 'num_foo' sounds more familiar than 'the foo count' or 'foo_count'. It certainly sounds more natural to me when referencing a quantity that isn't constantly changing. The word 'count', even when used as a noun, suggests to me a value that is going up over time.

Ruby and Python have .count methods, which demonstrate the word being used as a verb, rather than a noun. In Ruby you might say:

foos.count   # Count how many elements in the array 'foos'

Still, this returns a value representing the number of foos, which is exactly what you might expect if you just referenced a variable called 'foo_count'. So in some ways, the fact that 'foos.count' and 'foo_count' look similar is kind of nice.

'Number' can be ambiguous in some instances, since it's common to store numbers that don't represent a quantity of something. Other people have mentioned IDs and credit card numbers already. Here's another example:

num_string

Looking at that variable name, could you guess what it represents? Is it an integer representing the quantity of strings, or is it a string representation of a number?

So I'm just thinking out loud really, and giving some pros and cons for each as I see them. The reason I'm even on this old page is because I find myself using the two inconsistently and thought I'd see what other people are doing.

BTW, I don't like 'nr_foo', as 'nr' really doesn't suggest or sound like the word 'number' to me at all. It sounds like 'ner', or perhaps stands for 'not rated' or 'national rugby'. :-) And I won't even venture to say what fooCnt sounds like. Just no.