Naming Conventions for Number Foos Variables

Suppose I need to save the number of foo objects in a variable.

Not being a native English speaker, I always wonder what is the best (= short and immediately understandable) name for this var.

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

The full name should be number_of_foos , but it is a little verbose.

Which is your favorite and why?

+41
naming-conventions
Sep 18 '10 at 17:15
source share
10 answers

I go to fooCount because it is simple, and I think the word “count” is the shortest and best that describes it, and not “number” or the like.

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

I go to the account and call it using Foo.count, if you really need to save it as an attribute for the class you created, it is Foo.

readability for you and your team!

+15
Jan 25
source share

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

+18
Sep 18 '10 at 17:17
source share

In English, the words “number” and “count” can act as nouns or verbs, but it is probably more common to see a “number” used as a noun and “count” as a verb. Therefore, you can argue that “foos number” or “num_foo” sounds more familiar than “counting foo” or “foo_count”. This certainly sounds more natural to me when it refers to a quantity that does not change constantly. The word "account", even if it is used as a noun, offers me a meaning that grows with time.

Ruby and Python have .count methods that demonstrate that a word is used as a verb, not a noun. In Ruby, you can say:

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

However, this returns a value representing the number of foos, and this is exactly what you would expect if you just specified a variable called foo_count. So, in a sense, the fact that foos.count and foo_count look the same looks nice.

A "number" may be ambiguous in some cases, since it is common to store numbers that do not represent the quantity of something. Other people have already mentioned identifiers and credit card numbers. Here is another example:

 num_string 

Looking at this variable name, can you guess what it represents? Is this an integer representing the number of lines, or is it a string representation of the number?

So, I just think out loud and give some pros and cons for everyone, as I see them. The reason I am even on this old page is because I find that I use these two inconsistently, and thought I would see what other people are doing.

By the way, I do not like "nr_foo" because "nr" really does not offer or does not sound like the word "number" for me at all. It sounds like “ner,” or perhaps it means “not rated” or “national rugby." :-) And I won’t even dare say what fooCnt is. Simply no.

+8
Dec 31 '14 at 15:10
source share

fooCount if the variable is not constant, FOO_COUNT if there is one .: D

+7
Sep 18 '10 at 17:19
source share

The Linux kernel uses "nr_foo", which is better than "no_foo" (this is like negation). I myself tend to use "fooCount" or "fooCnt", but also sometimes "numFoo". I'm not sure why I hesitate between "fooCount" and "numFoo". I think it depends on my mood. But you, you have to be consistent (like me);)

+6
Sep 18 '10 at 17:22
source share

Basically fooCount , as everyone said. Sometimes it’s better to use foos , usually when you don’t actually have a list of foos, or they are not separate objects (for example, seconds , you can have slices for pizza, etc.)

Use foos only when there is no chance of confusion - when it is obvious that in this context you will never have a list of foos.

+2
Sep 18 '10 at 17:26
source share

I would go for fooCount

+1
Sep 18 '10 at 17:17
source share

I tend to use fooCount or the like.

+1
Sep 18 '10 at 17:17
source share

I personally would go for total_foos or totalFoos depending on the standard language. It shows better that the value is the total, and not just a counter.

It also makes sense to say “I have 3 common foos” rather than “I have 3 foos accounts”.

All in all, this is not a huge deal, but I always use total over count !

+1
Mar 30 '17 at 13:38 on
source share

I use for the quantity somethings: somethingCount ( something_count )

I use somethings: somethingIndex ( something_index ) for the serial number, because the word "number" is ambiguous (this means the number and serial number)

0
Oct 12 '16 at 9:17
source share



All Articles