122

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?

Giacomo
  • 11,087
  • 5
  • 25
  • 25
  • I liked the fooCount answer, but I don't think number_of_foos is too long, but I suppose it depends what "foo" actually is. – Chance Mar 25 '11 at 15:58
  • And also what you want to do with the variable. Arithmetic expressions with `number_of_foos` in it become cumbersome quickly. – user7610 Dec 09 '16 at 13:21
  • Possible duplicate of https://stackoverflow.com/questions/6358588/how-to-name-a-variable-numitems-or-itemcount?noredirect=1&lq=1 – Michael Freidgeim Jun 16 '21 at 13:55

10 Answers10

79

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!

moffeltje
  • 4,521
  • 4
  • 33
  • 57
ironmaurus
  • 918
  • 8
  • 8
  • `foosCount`, since plural even better represents a set. And it is supposed to be a count of a set of things. – Magne May 01 '20 at 16:42
  • 2
    But it depends. Plural sounds good for some names like `gamesIncludedCount`, but singular sound better for other names such as `gameCount`. – Magne May 01 '20 at 16:49
32

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

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
22

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.

Kal
  • 2,098
  • 24
  • 23
17

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) ;)

siride
  • 200,666
  • 4
  • 41
  • 62
  • 4
    @rjack - "number" is vague, "count" is specific. A number could be an ID, for example. [I think McConnell makes this point in "Code Complete," but I don't have a copy with me.] Abbreviations of "number" more likely understood (other than by Linux kernel writers) include "nFoo" and "numFoo." – Andy Thomas Sep 19 '10 at 14:57
  • 1
    @Andy Thomas-Cramer - Not so confusing IMHO: `credit_card_nr` sounds like "credit card number", while `nr_credit_cards` sounds like "number of credit cards" and it's shorter than `credit_cards_count`. Sounds quite natural to me (but I may be wrong since I'm not a native english speaker). – Giacomo Sep 19 '10 at 21:04
  • 1
    @rjack It would be credit_card_count anyway, since you don't generally pluralize nouns when they are part of a compound. – siride Sep 20 '10 at 00:20
  • 1
    The Linux's "nr" and your answer is a good example of how confusing the word "number" is. In Linux, "nr" is (also?) used to mean the sequential number instead of count. E.g. `__NR_open` isn't the number of `open` system calls — it's the index of the system call, i.e. the value you pass to the system call instruction to specify which system call you're invoking. – Ruslan Jun 27 '19 at 05:27
  • 2
    As in @Kal's answer, I would personally avoid `fooCnt` due to the potential to interpret it wrongly. – Ruben9922 Nov 04 '19 at 17:06
  • I agree with Ruben. I'd definitely read fooCnt the wrong way – asaf92 Aug 01 '20 at 09:42
8

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

Gordon Gustafson
  • 40,133
  • 25
  • 115
  • 157
7

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!

Will
  • 464
  • 5
  • 7
  • 3
    `totalFoos` could imply that it is a _complete set_ of Foos (it happened in my case, when I was collecting various sets). Also, it is nice to have the identifier at the beginning of the word, for readability. So I'd go with `foosCount`, as it is less ambivalent and starts with `foo*`. You could also say "I have a foosCount of 3". – Magne May 01 '20 at 16:42
3

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.

configurator
  • 40,828
  • 14
  • 81
  • 115
  • If you use `foos` sometimes to mean "the number of `foo` objects" and other times to mean "the collection of `foo` objects, I'd think that could lead to confusion. – James McNellis Sep 18 '10 at 17:31
  • `slices` sounds to me like an array of `slice` objects, that are part of a `pizza` object. I'd use `sliceCount`, or `nr_slices`, or whatever (I haven't decided yet :D) – Giacomo Sep 18 '10 at 17:32
  • Yes, only use that when there's no chance of condusion – configurator Sep 18 '10 at 17:42
2

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)

artamonovdev
  • 2,260
  • 1
  • 29
  • 33
1

I'd go for fooCount

CyberDude
  • 8,541
  • 5
  • 29
  • 47
1

I tend to use fooCount or similar.

Engin Kurutepe
  • 6,719
  • 3
  • 34
  • 64