6

Simply put, is it a good idea to name collections and composite objects using plurality?

class PandaBears {
   PandaBear[] bears;

   class PandaBear {
   }
}

My concern is that the class names are quite similar. On the other hand, PandaBearList reveals the implementation, but is more easily distinguishable.

Garrett Hall
  • 29,524
  • 10
  • 61
  • 76
  • I don't know if it's good practice or not. But I don't like using plurals. I'd use `PandaBearCollection`. – atoMerz Jan 23 '12 at 14:30
  • IMO, if I have some special methods (such as returning some elements with specific values, etc) or I want a strict API with only methods that I allow user to use with the collection, otherwise, I would re-use Collection, List or Set (java). – Genzer Jan 23 '12 at 14:39
  • 1
    I prefer conceptual names. What's the role of your outer class? Think about it's purpose and pick an appropriate name. PandaBearForest? PandaBearMom? PandaBearCage? PandaBearGroup? Maybe you don't need the outer class at all. – Nick Dandoulakis Jan 23 '12 at 14:53
  • 1
    Looks related to: http://stackoverflow.com/questions/1126256/net-collection-naming-convention – S2S2 Jan 23 '12 at 14:55

3 Answers3

2

I would avoid plurality.

If you don't want to include the suffix List, you could always use the suffix Collection which is a standard convention and does not reveal the implementation details. Of course this depends on the language you are using.

There is also a C#-specific work-around which I like to use if the structure is not very complex. You can avoid creating the Collection class at all, by declaring all the methods as extension methods of the related IEnumerable<T>. So, in this case, you could declare extension methods on IEnumerable<PandaBear>, provided that your collection does not have other private variables.

linepogl
  • 9,147
  • 4
  • 34
  • 45
2

I would prefer PandaBearCollection. A class name that is a countable noun just agrees better with the fundamental metaphor of OOP, an "object".

For example, try describing the signature of the following two functions:

void func(PandaBearCollection collection1, PandaBearCollection collection2);
void func(PandaBears pandaBears1, PandaBears pandaBears2);

The first one would naturally be: "A function that takes two collections of panda bears".

What would be the second one? "A function that takes two panda bears"? No, it just doesn't work.

G S
  • 35,511
  • 22
  • 84
  • 118
1

I prefer using plurality because as you mention it does not indicate the implementation, which could easily change in the future. Another consideration on the naming convention is if you are using an ORM and what type of convention will it use when translating your database schema, as discussed here. You will probably get a lot of advice both ways. I think what is more important is that you pick a convention that works for you and your team and you stick with it.

Community
  • 1
  • 1
Kevin Junghans
  • 17,475
  • 4
  • 45
  • 62