3

I am a C++ programmer from a non-English country.I am always confused about how to choose one of the following function names:

GetCountOfObjects

GetNumberOfObjects

GetObjectCount

Who can tell me what the subtle differences are between them?

Bart
  • 19,692
  • 7
  • 68
  • 77
xmllmx
  • 39,765
  • 26
  • 162
  • 323

5 Answers5

4

I'm also a programmer from non-English country, but I think the best way to choose the name is

  1. use the name that is the most clear
  2. use the shortest name enough to understand easily
  3. Also, english language suppose that it's better to swap the order than use 'Of'.

So, IMHO the best variant is 'GetObjectCount' here, of course if it returns the quantity of object.

Vladimir Ivanov
  • 42,730
  • 18
  • 77
  • 103
3

Use whatever you want, but use it consistently.

TBH
  • 451
  • 2
  • 9
  • Thank you for your quick response. I know all of them are acceptable. But I just wonder what the SUBTLE differences are between them. – xmllmx Dec 09 '10 at 09:00
  • I believe the second one is the most accurate, because it is the closest to the natural language (since you want to get numer of objects!) – TBH Dec 09 '10 at 09:02
3

GetNumberOfObjects probably sounds closest to natural English. GetCountOfObjects sounds slightly awkward. Other than that, there is almost no difference.

My personal style would probably be to use GetNumberOfObjects for a method that just returns a known number, but CountObjects for a method that actually performs the counting.

EDIT: The reason for this difference, at least to me, is that the word 'number' is more commonly used as a noun while 'count' is more commonly used as a verb.

Really, this is a style choice. Use whatever you choose consistently and it will be fine.

DGH
  • 11,189
  • 2
  • 23
  • 24
  • Thank you for your explanation. I also think GetNumberOfObjects sounds more natural, but Microsoft uses GetTickCount rather than GetNumberOfTicks, which makes me confused. – xmllmx Dec 09 '10 at 09:05
  • 1
    @xmllmx: I think @Vladimir Ivanov's answer provides one other clue: short names are often considered better. Also, Number may be used more for things that are more static or constant (numberOfApplesInBasket, Car.GetNumberOfWheels, etc.) while Count may be preferable for values that are changing or regularly incremented (countSecondsSinceMidnight, GetTickCount, etc.) – DGH Dec 09 '10 at 09:09
  • thank you for your convincing answer. Your advice is clear and easy to follow. – xmllmx Dec 09 '10 at 09:26
  • 1
    @xmllmx and @DGH - "Number" is vague, "count" is specific. I think McConnell made this point in "Code Complete." It is common to use "count" as both a verb and a noun in English. "GetXCount" allows an alphabetic listing to group all the "GetX*()" methods. – Andy Thomas Dec 09 '10 at 16:56
0

I would go for the shortest simplest: size() if it makes sense. That is, if you are trying to add a member function to a class that somehow resembles a container, using the same names that are used in existing libraries for the same concepts will make code simpler to read.

Even if that does not make sense, while in Java getters and setters are common, in many C++ libraries the same function names will drop the get part and provide a shorter name: GetNumberOfObjects => NumberOfObjects, GetObjectCount => ObjectCount... If you want to make your object different from containers (and thus you explicitly want to avoid size()) I would probably go for objectCount or numObjects. While numObjects is not proper english it is easy to read and interpret and it is short.

David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
-9

use whichever u feel comfortable wid but be consistent wid it.avoid very long names as u can err.also u can use sum kind of distinction in d names 2 help u figure out type of variable or whether it is static,local or public or private

lovesh
  • 5,235
  • 9
  • 62
  • 93
  • 9
    Down-voted for misspelling and text-speak. Especially on questions like this where the asker is a non-English speaker, clear and correct spelling and grammar are important. You wouldn't type like this in documentation or a professional report, would you? – DGH Dec 09 '10 at 09:11
  • @DGH: Wow, I've never seen any answer take such a huge beating for poor typing. – BoltClock Dec 10 '10 at 22:37