84

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?

4
  • Would the class contain a repository for multiple customers? If it's one repository for one customer...
    – JeffO
    Aug 25, 2011 at 21:36
  • 2
    @Jeff O, repository can contain GetCustomerById and GetCustomers methods at the same time Aug 25, 2011 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, 2011 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 at 19:53

4 Answers 4

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.

4
  • 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, 2011 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. May 13, 2012 at 3:26
  • Instead of Collection how about Iterator?
    – Julian
    May 4, 2018 at 11:46
  • @Julian An Iterator would be some kind of co-routine rather than a data structure for holding multiple objects, right? Jul 16, 2018 at 13:17
92
+50

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.

6
  • 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, 2019 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, 2019 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, 2020 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, 2020 at 7: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, 2020 at 10:21
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.

2
  • 1
    And to think, all this time I've been using Peoples collections!
    – Jordan
    Aug 26, 2011 at 5:18
  • 2
    List<person> people ... seems ok to me, @Jordan.
    – StevenV
    Aug 28, 2011 at 14:37
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.

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