Naming conventions for “number of foos” variables [closed]

ⅰ亾dé卋堺 提交于 2019-12-20 09:46:07

问题


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?


回答1:


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!




回答2:


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




回答3:


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




回答4:


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.




回答5:


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




回答6:


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!




回答7:


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.




回答8:


I'd go for fooCount




回答9:


I tend to use fooCount or similar.




回答10:


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)



来源:https://stackoverflow.com/questions/3742650/naming-conventions-for-number-of-foos-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!