5

As a non-native English speaker, I often wonder about using the plural form in the initial part(s) of noun phrases when naming classes or objects.

For example:

  • If I have an interface to multiple [streaming] players, it is good English to name it PlayersInterface, as opposed to PlayerInterface which is an interface to one player?

  • If there is a service that handles events, is it ok to name it EventsService? Or does EventService sound significantly better?

Thank you for your help!

EDIT:

  1. Obviously in .NET an interface name would start with an I. So let's change the example a bit and call it PlayersGateway.

  2. I don't really have another class named PlayerInterface (or PlayerGateway). It was just an example of an alternative name I would use if I only needed an interface to one player. I think that using both PlayerGateway and PlayersGateway in the same project is hard to maintain, not to say evil to future team members. So please assume there is no PlayerGateway, just PlayersGateway.

Ilya Kogan
  • 21,995
  • 15
  • 85
  • 141
  • EventService use case is in my opinion simple and clear - both forms are gramatically correct and it depends on your naming convention. What I would like to know is: WidgetsList or WidgetList ? – tillda Feb 17 '11 at 14:55
  • 2
    Not a C# guy, but wouldn't that be `IPlayer`and `IPlayers`? Also sounds way better imho. –  Feb 17 '11 at 14:57
  • 3
    I personally think life is easier if WidgetList is used only for List, WidgetsList could be used for List>. In response to the original question you might choose a name like 'MultiPlayerInterface" – Jimmy Feb 17 '11 at 15:00
  • I've never seen the plural used in the manner of WidgetsList, even though that would have to be considered the correct name. Let's face, nobody creates EventService to handle just one (or one kind of) event, so it really SHOULD be EventsService -- it just never is. – Rich Feb 17 '11 at 15:07

4 Answers4

8

It is my understanding that you should generally use the singular phrasing of a word. For instance if you had a class Car, and you had a class that contained many instances of class Car you would call it CarCollection instead of Cars

Brent
  • 1,378
  • 2
  • 16
  • 30
  • 3
    I agree, although I would add that if you had a collection of them, e.g. List, it's Ok to call that *variable* 'cars'. – Grant Crofton Feb 17 '11 at 14:59
  • true, provided you don't confuse yourself, I've certainly ran into issues with variables/collections where the only naming difference is one character – Brent Feb 17 '11 at 15:01
6

In English, when you stick a noun next to another noun to modify it, you usually use the singular:

 Coat Room
 Guard Tower

even though there may be several coats in the room or guards in the tower. IMHO, variable naming should follow the general rules of the language.

dandan78
  • 13,328
  • 13
  • 64
  • 78
1

I'd tend to use the singular. EventHandler or EventService would have the connotation that it would handle one event after another. For the video players, I might be convinced if you said that you were connecting to multiple players and controlling them all at pretty much the same time.

Would you have a second type/variable for the singular case?

PlayersInterface AllPlayers = new PlayersInterface();
PlayerInterface MyPlayer0 = AllPlayers.getPlayer(0);
PlayerInterface MyPlayer2 = AllPlayers.getPlayer(1);

Even then, I think the two type names are too similiar.

But, it's personal preference.

Tom Cerul
  • 1,773
  • 2
  • 13
  • 27
  • Thank you, @Tom, but this is not exactly what I have in mind, I think I didn't phrase the question so well. I edited the question in order to clarify what I meant. – Ilya Kogan Feb 17 '11 at 15:13
1

Singular form often makes more semantical sense for class names and table names in databases, because each instance (or row in a database) is a singularity.

You can apply the same reasoning on arrays as well. You can chose to think of parcel[4] as parcel #4 and not number 4 of a bunch of parcels.

IMHO, EventService and PlayerInterface are better names that sound better to an english ear.

MattBianco
  • 1,501
  • 2
  • 20
  • 30