12

Generally variables are named in the singular Car car; and arrays and list with the pluras: List<Car> cars; or Car[] cars; But what about nouns like fish or media* that are already plural?

*already lost the battle to have the class called Medium.

3
  • 4
    "fishes" is an acceptable plural form of of "fish", at least.
    – Izkata
    Jan 21, 2014 at 2:52
  • 3
    I often use carList / carArray
    – Bryan Chen
    Jan 22, 2014 at 3:13
  • I have to agree with @BryanChen. It is very easy to use standard English plurals but being a natural language there are many irregular words. Programming is logical. If ever there needed to be a way to automate getting or setting of words, it would be very difficult to do with standard pluralization. I support using carList, fishList, etc. Sep 11, 2018 at 10:17

3 Answers 3

12

There are a couple of ways to tackle this problem. None of them are perfect, but you can find a workable solution. The key is that your team needs to come up with a standard approach to resolving this and to stick with it.

Here are some of the options:

  • Different capitalization

The singularly named variable will start with one case (lower case) while the plural variable will start with the other case (upper case).

 var Fish = new List<fish>();  //plural  
 var fish = Fish[0];           //singular
  • Deliberate use of improper grammar

Example:

 var Fishes = new List<fish>();  //plural  
 var fish = Fishes[0];           //singular  

or

 var Meese = new List<Moose>();  //plural  
 var Moose = Meese[0];           //singular
  • Use group name instead

Example:

 var SchoolOfFish = new List<Fish>();  //plural  
 var Fish = SchoolOfFish[0];           //singular
1
  • 1
    Uppercase names are usually reserved for types/classes, etc!
    – basseur
    Feb 21 at 16:56
5

My suggestion would be to try to use a more meaningful name for the list variables. I know this is sometimes hard to do, though.

For example, instead of List<Car> cars, use something like List<Car> parkedCars. Instead of Fish[] fish, use something like Fish[] availableFish.

4
  • but each fish in the array of availableFish is also an availableFish. I don't think is a great solution.
    – Totty.js
    Jun 29, 2017 at 16:24
  • The point is to use descriptive words to compose variable names. If you name the array availableFish and need another variable to hold one of the items in the array, well then you just need to choose a different appropriate descriptive word. Perhaps selectedFish would work. If not that, then something else. Use a thesaurus to find the right word. If nothing else, you could even just use fish for a limited-scope variable, because the name wasn't already taken for the array. Jul 5, 2017 at 18:21
  • @totty-js No, there is no "array of availableFish" in the code above. An array of availableFish-s means availableFish[] insertvariablenamehere;. The only reason you're creating confusion is because you're using an inaccurate English description of the code. Some accurate English readings of the code include: "there is an array, availableFish", "there is an array named availableFish", "there is a Fish array named availableFish which contains the available Fish objects"...
    – naki
    Jul 26, 2017 at 23:28
  • A correct sentence that follows the same form as the sentence in your comment would be "each Fish in the array availableFish is a Fish".
    – naki
    Jul 26, 2017 at 23:54
4

I think names like List<Fish> fish are perfectly fine. We use "fish" as both singular an plural in writing; why not do the same in code?

If you want a singular Fish fish in the same scope, say for iterating, can always switch to List<Fish> fishList or List<Fish> listOfFish.

4
  • 6
    List<Fish> school Jan 21, 2014 at 3:10
  • 3
    mostly because often you haves code conventions like for( Book book : books ) so the singular is already taken or going to be used for something else.
    – Sled
    Jan 21, 2014 at 14:44
  • fishList or listOfFish is not perfect because it is coupled to the implementation. If List is changed to Collection or Set the name implies something that is no longer the case.
    – basseur
    Feb 21 at 16:20
  • @basseur I see your point, but it's not really an implementation detail, since List and Set are interfaces with somewhat different properties. Arguably it would be appropriate to rename fishListfishSet to reflect the change in semantics. Feb 21 at 20:26

Not the answer you're looking for? Browse other questions tagged or ask your own question.