Naming conventions for “number of foos” variables

2019-03-14 10:10发布

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?

10条回答
你好瞎i
2楼-- · 2019-03-14 10:14

I tend to use fooCount or similar.

查看更多
Explosion°爆炸
3楼-- · 2019-03-14 10:15

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!

查看更多
对你真心纯属浪费
4楼-- · 2019-03-14 10:19

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)

查看更多
Rolldiameter
5楼-- · 2019-03-14 10: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.

查看更多
时光不老,我们不散
6楼-- · 2019-03-14 10:26

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.

查看更多
smile是对你的礼貌
7楼-- · 2019-03-14 10:28

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

查看更多
登录 后发表回答