Naming conventions for "number of foos" variables

Naming Conventions

Naming Conventions Problem Overview


Let's suppose that I need to store the number of foo objects in a variable.

Not being a native English speaker, I always wonder what's the best (= short and immediately clear) name for that var.

foo_num? num_foo? no_foo? foo_no? or something else?

The full name should be number_of_foos, but it's a bit verbose.

What's your favorite and why?

Naming Conventions Solutions


Solution 1 - Naming Conventions

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!

Solution 2 - Naming Conventions

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

Solution 3 - Naming Conventions

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.

Solution 4 - Naming Conventions

The Linux kernel uses "nr_foo", which is better than "no_foo" (that looks like a negation). I myself tend to use "fooCount" or "fooCnt", but also sometimes "numFoo". I'm not sure why I vacillate between "fooCount" and "numFoo". Guess it depends on my mood. But you, you should be consistent (as should I) ;)

Solution 5 - Naming Conventions

fooCount if the variable isn't a constant, FOO_COUNT if it is. :D

Solution 6 - Naming Conventions

I personally would go for total_foos or totalFoos depending on the language standard. It represents better that the value is a final total and not just a running count.

It also makes more sense to say "I have 3 total foos" rather than "I have a 3 count of foos".

Overall, it's not a huge deal but I always use total over count!

Solution 7 - Naming Conventions

Mostly fooCount like everybody said. Sometimes it is more appropriate to use foos, usually when you don't actually have the list of foos, or they aren't separate objects (e.g. seconds; for a pizza you can have slices, etc.)

Only use foos when there's no chance of confusion though - when it's obvious that you'd never have a list of foos in this context.

Solution 8 - Naming Conventions

I use for the quantity of somethings: somethingCount (something_count)

I use for the sequence number of somethings: somethingIndex (something_index), because the "number" word is ambiguous (it means the quantity and the sequence number)

Solution 9 - Naming Conventions

I'd go for fooCount

Solution 10 - Naming Conventions

I tend to use fooCount or similar.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGiacomoView Question on Stackoverflow
Solution 1 - Naming ConventionsironmaurusView Answer on Stackoverflow
Solution 2 - Naming ConventionsJustin NiessnerView Answer on Stackoverflow
Solution 3 - Naming ConventionsKalView Answer on Stackoverflow
Solution 4 - Naming ConventionssirideView Answer on Stackoverflow
Solution 5 - Naming ConventionsGordon GustafsonView Answer on Stackoverflow
Solution 6 - Naming ConventionsWillView Answer on Stackoverflow
Solution 7 - Naming ConventionsconfiguratorView Answer on Stackoverflow
Solution 8 - Naming ConventionsartamonovdevView Answer on Stackoverflow
Solution 9 - Naming ConventionsCyberDudeView Answer on Stackoverflow
Solution 10 - Naming ConventionsEngin KurutepeView Answer on Stackoverflow