Is it good practice to use plurality to refer to collections?

Simply put, is it useful to name collections and composite objects using plurality?

class PandaBears { PandaBear[] bears; class PandaBear { } } 

My concern is that the class names are very similar. PandaBearList , PandaBearList other hand, shows an implementation, but it is easier to recognize.

+6
source share
3 answers

I would prefer PandaBearCollection . The class name, which is a countable noun, is more consistent with the fundamental metaphor of OOP, the β€œobject”.

For example, try to describe the signature of the following two functions:

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

The first, of course, will be: "A function that takes two collections from panda."

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

+2
source

I would avoid multiplicity.

If you do not want to include the List suffix, you can always use the Collection suffix, which is a standard convention and does not disclose implementation details. Of course, it depends on the language you use.

There is also a C #-related work environment that I would like to use if the structure is not very complex. You can never create a Collection class by declaring all methods as extension methods of the associated IEnumerable<T> . Thus, in this case, you can declare extension methods on IEnumerable<PandaBear> , provided that your collection does not have other private variables.

+1
source

I prefer to use plurality because, as you mention, this does not indicate an implementation that could easily change in the future. Another consideration regarding the naming convention is that if you use ORM and what type of convention will be used when translating the database schema, as discussed here . You will probably get a lot of advice in both directions. I think it’s more important that you choose an agreement that works for you and your team, and you stick to it.

+1
source

Source: https://habr.com/ru/post/906670/


All Articles