How to name a variable: numItems or itemCount?

前端 未结 5 448
暖寄归人
暖寄归人 2021-02-01 17:01

What is the right way to name a variable

int numItems;

vs.

int itemCount;

or constant:

public         


        
相关标签:
5条回答
  • 2021-02-01 17:22

    It actually depends on you. The two types of naming conventions are camelCase and snake_case

    As you can identify from the naming, camel case has one small letter in the initial part of the variable followed by the Capital words

    Eg itemCount.

    snake case is a continuous word with an underscore ' _ ' in between the words Eg item_count

    As far as the naming is concerned, numItems is quite confusing for others to read. But itemCount is a good name for a counter variable

    0 讨论(0)
  • 2021-02-01 17:27

    It's a matter of personal preference, just make sure you are consistent throughout your code. If you're working with others check what's been done in existing code.

    For the constant I would find MAX_ITEMS more logical than MAX_NUM_ITEMS or similar, it just sounds better to me.

    0 讨论(0)
  • 2021-02-01 17:36

    In "Code Complete," Steve McConnell notes that "Number" is ambiguous. It could be a count, or an index, or some other number.

    "But, because using Number so often creates confusion, it's probably best to sidestep the whole issue by using Count to refer to a total number of sales and Index to refer to a specific sale."

    0 讨论(0)
  • 2021-02-01 17:45

    For Java I would use itemCount and MAX_ITEM_COUNT. For Ruby, item_count and MAX_ITEM_COUNT. I tend not to use names that can be interpreted wrongly (numItems may be a shortcut for numerate_items or number_of_items), hence my choice. Whatever you decide, use it constantly.

    0 讨论(0)
  • 2021-02-01 17:47

    item_count or itemCount (there's a religious war brewing there, though)

    0 讨论(0)
提交回复
热议问题