8

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.

closed as primarily opinion-based by durron597, gnat, user40980, user22815, Dan Pichelman Aug 5 '15 at 18:06

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 3
    "fishes" is an acceptable plural form of of "fish", at least. – Izkata Jan 21 '14 at 2:52
  • 2
    I often use carList / carArray – Bryan Chen Jan 22 '14 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. – Damien Golding Sep 11 '18 at 10:17
9

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
3

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.

  • 6
    List<Fish> school – Adrian Schneider Jan 21 '14 at 3:10
  • 2
    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. – ArtB Jan 21 '14 at 14:44
2

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.

  • but each fish in the array of availableFish is also an availableFish. I don't think is a great solution. – Totty.js Jun 29 '17 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. – Dr. Wily's Apprentice Jul 5 '17 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 '17 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 '17 at 23:54

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