83

It is always difficult for me to choose between singular and plural forms for classes names:

CustomerRepository vs. CustomersRepository
CustomerService vs. CustomersService
CustomerController vs. CustomersController

And for composite names it is even more difficult:

OrderCustomerRepository vs. OrderCustomersRepository vs. OrdersCustomersRepository

What approach do you prefer and why?

SiberianGuy
  • 4,753
  • 6
  • 34
  • 46
  • Would the class contain a repository for multiple customers? If it's one repository for one customer... – JeffO Aug 25 '11 at 21:36
  • 2
    @Jeff O, repository can contain GetCustomerById and GetCustomers methods at the same time – SiberianGuy Aug 25 '11 at 21:37
  • 4
    In Data Modeling, a table name should be singular. If your application is using domain classes that correspond 1-1 to your data model then it may be a good idea to keep the naming singular. Of course mapping classes to tables in 1-1 fashion may not be the best way to build your object model for an OO application, but this is beside the question. – NoChance Aug 25 '11 at 23:26
  • If this question is closed for being "opinion-based" here, in the _Software Engineering_ section (never mind the 81 upvotes either...), then I have to recalibrate my expectations about it in general (and be careful never to ask a difficult to formalize "epistemic" question here...). – Sz. Feb 27 '23 at 19:53

4 Answers4

92

Use singular. The tool to turn screws with is called "screw driver" not "screws driver".

However, pluralize your method and property names accordingly, to indicate whether one value or a collection of them will be returned.

jmq
  • 6,048
  • 5
  • 28
  • 39
back2dos
  • 29,980
  • 3
  • 73
  • 114
  • 4
    Thats a weak argument: in german you actually pluralize the screw in screw driver. For good reason: we dont throw the tool away after using it for the one screw. Still agree with pluralization. – Mathias F Nov 20 '19 at 14:36
  • 1
    German is irrelevant, since it's not being discussed here, but still: by your "logic" you would throw away a Zahnbürste after brushing a single tooth with it. It is fallacious to speak of the plural in German composites: in "Schraubendreher" the "Schrauben" may be morphologically identical with the plural, but semantically it is not, as seen in "Schraubenkopf" which does not in fact suggest multiple screws sharing the same head. It's a "Fugenmorphem" and carries no information about quantity. Still: this is English, which luckily (for once) has a much simpler rule to be followed ;) – back2dos Nov 22 '19 at 17:39
  • Its a wrong example and argument altogether. ScrewDriver does not contain screws. ScrewDriver is associated with screw. CustomersRepository is a facade that encapsulates all customers. CustomerRepository thus aggregates(contains) customers. – Legolas21 Jul 30 '20 at 14:07
  • You've written "CustomersRepository" *and* "CustomerRepository", so it's hard to tell what you're even getting at. Also, you're cherry picking. The question revolves around `CustomerController` and `CustomerService` too, and neither *contain*. customers But even if the question were what you make it out to be, let me just point out that noodle soup *contains* more than one noodle and a cookie jar *contains* more than one cookie (unless the cookie monster was involved) and a ball pit *contains* more than one ball (otherwise it'd hardly be any fun). – back2dos Jul 31 '20 at 07:59
  • Sorry, was a typo. Meant to type CustomersRepository. I don't have enough points to edit my own comment :). Could you please help me with it kind Sir? I had intentionally cherry picked CustomersRepository to demonstrate that, ScrewDriver example might not hold in every case. Personally, I would have gone with CustomersRepository - contains many customers at any point, CustomerService - serves a single customer inside a context(request/thread) But, you make a valid point with noodle soup,cookie jar, ball pit :) Maybe I overthink and should just apply KISS principle in thinking :) – Legolas21 Jul 31 '20 at 10:21
  • @Legolas21 Bad example yes, but the conclusion seem to hold anyway. For example you store your tools in a "tool box". However note that the way it's named is crucial. If you call it "container of *" the element would be pluralized and of course if you only use the elements as naming of the term they would of course be pluralized. – skyking May 23 '22 at 14:23
88

The only thing I pluralize is collections.

foreach (var customer in customers)
{
    // do something with customer
}

All of your examples are individual objects, so they are not pluralized. Yes, the names refer to objects that might have multiple instances, but all you need to know in the name is the object entity (i.e. customer).

So in all of your examples, the singular is the correct form. Makes life much easier.

Robert Harvey
  • 198,589
  • 55
  • 464
  • 673
  • 2
    I'd use `customerCollection` for collections, but for the rest of it I agree. This is a best-practice in database design and works quite good in code also. – Jan_V Aug 28 '11 at 12:24
  • 14
    I dislike words like "Collection" here. English has a perfectly good way of referring to a collection of things, using plurals. – Gort the Robot May 13 '12 at 03:26
  • Instead of `Collection` how about `Iterator`? – Julian May 04 '18 at 11:46
  • @Julian An `Iterator` would be some kind of co-routine rather than a data structure for holding multiple objects, right? – Lorraine Jul 16 '18 at 13:17
13

Definitely singular. You don't create an object of People, you create a collection of Person objects. The only time I would use plurals would be for static classes, i.e. SupportServices, StringUtils, etc. However in this case, the class acts more as a namespace than anything else.

Rob
  • 430
  • 2
  • 10
3

Remember, a class is a template for an object. So think about the object that you are referring to.

Often it is a singular entity, especially when it is an ORM entity. Sometimes it could be a collection.

I believe the answer is specific to the context.

indi
  • 141
  • 2