3

I'm new to learning Java and I'm bad at English but I try my best to write good, understandable source code.

I want to make variables to save the "number of cars" or "number of items". How do I abbreviate "number of ..." without using symbols like # that don't work in a source code?

Thanks

javachecker
  • 43
  • 1
  • 3
  • 3
    I wouldn't abbreviate it at all. Readability is important. I'd call it "numberOfCars" or "numberCars". – Modus Tollens Feb 16 '20 at 18:53
  • 5
    `carCount`, `itemCount`. Because a "count" and the "number of" something, are synonyms. – Elliott Frisch Feb 16 '20 at 18:55
  • Or maybe ```numItems``` and ```numCars```. Using clear speaking names is always better than abbreviations. The programming language supports very long names, so use them to make your code well readable. – Stefan Feb 16 '20 at 18:55
  • +1 to both not abbreviating and `count`. I would still prefer the latter as with the former (especially in Java with some relatively verbose class names), you might end up going over the standard line width and/or have to put a line break in the variable definition. A long line that does not fit on your screen is just as bad as one that is strewn about a couple lines, IMO. – filpa Feb 16 '20 at 19:00
  • For whatever it's worth, Google's public API guidelines suggest the name ["`car_count`"](https://aip.dev/141), which would be `carCount` in Java. But, er, YMMV. – Andy Turner Feb 16 '20 at 19:21

2 Answers2

3

You have several choices to do that

  1. numberOfItems (verbose, but clear in meaning)
  2. numItems (it's ok)
  3. itemCount (probably, the best — what I'd have used)
  4. items (shortest, but can't know if it is an integer or a list of items)
pavi2410
  • 1,220
  • 2
  • 12
  • 16
  • 1
    sounds silly, but wouldn't you write "itemsCount" because it's multiple items? As I said, bad at English... – javachecker Feb 16 '20 at 19:04
  • 1
    A quick search on that pointed me to [this page](https://www.usingenglish.com/forum/threads/242655-Plural-or-singular-when-using-with-the-word-count) where the discussion leaned towards singular (I just skimmed through it). But ultimately, it's your choice. – pavi2410 Feb 16 '20 at 19:12
0

I try my best to write good, understandable source code

Then my best advice would be not to abbreviate variable names.
Just go with numberOfCars.

Why? I know you've probably seen a lot of programs where people use one-letter variables or stuff like numCars.
Abbreviating your variables make your code less clear for others (including you in 6 months).
We all have great text editors with auto-completion on variables, use that.

grandouassou
  • 2,500
  • 3
  • 25
  • 60
  • I can understand that using no abbreviation would be best but it feels like a pretty long name. Also the significant part to use auto complete is at the very end so you always have to type in "numberOfC..." before you can hit Enter button. Is there no good solution to this problem? – javachecker Feb 16 '20 at 18:58
  • 1
    If you use IntelliJ, you can type n-o-c to get numberOfCars in the auto-complete suggestions – pavi2410 Feb 16 '20 at 19:01
  • I usually type the first 3 letters and then use the arrows to go through the list. If the list is big I use @Pavitra technique. – grandouassou Feb 16 '20 at 19:04