balexandre
balexandre

Reputation: 75103

When to use Plural vs Collection word on Methods

I'm creating an API for a module and after I created several methods inside my classes, I asked myself this question.

Right now, and as an example, I'm doing this:

public Company GetMonitoredCompany( String companyName ) { ... }
public List<Company> GetMonitoredCompanies( ) { ... }

But I realize that for several times that I use other API's / Services sometimes they have Collection in the name maybe like:

public List<Company> GetMonitoredCompanyCollection( ) { ... }

is there a rule for this? a pattern? or either way should be ok?

Upvotes: 4

Views: 2621

Answers (6)

Alex
Alex

Reputation: 36586

I find it very confusing when only using a plural to distinguish a collection. I normally use List instead of Get when returning collections so the example would be

ListMonitoredCompanies

This makes it very easy to use intellisense as the add, update, delete, list and get functions all have their own starting letter.

Upvotes: 1

D&#39;Arcy Rittich
D&#39;Arcy Rittich

Reputation: 171451

If it can possibly return multiple values (collection, array, etc), use a plural (GetMonitoredCompanies), else use a singular. Do not include the returned data type in the name. This is just asking for trouble where you change the return data type and forget to rename the method, sowing mad confusion.

Upvotes: 1

Wilka
Wilka

Reputation: 29613

For this case, I would copy the .NET Framework naming. E.g. Directory.GetFiles and Type.GetMembers and go with the plural. Unless doing so makes the code unclear, obviously.

Upvotes: 0

Mat
Mat

Reputation: 86504

Never do the Collection thing - unless you feel it adds significant meaning. Modern tools are able to easily tell you at a glance what return type to expect. What happens when you change the return type? You either leave the function name wrong, or have to change it throughout your code (albeit you may need to do that anyway, but if you subclass, etc. you may not need to otherwise).

For the same reason as hungarian notation is (controversially) no longer particularly useful in high-level languages.

In all cases, be consistent across your code.

Oh, and don't be stupid. I had a colleague (in a moment of annoyance I think) call his functions along the lines of FunctionThatLoadsTheTotalFromFileNumberCount in an attempt to convey the function return type (Number), meaning (Count), that it was a function (Function) and what it did actually did, rather verbosely.

Upvotes: 0

Chris
Chris

Reputation: 1705

Use the shortest, simplest name that clearly shows the methods purpose. And be consistent within your project.

In this example, if there are no other considerations, I would use the name:

GetMonitoredCompanies

Because it's shorter and clearer.

(I would also return a read-only ICollection or IEnumerable unless you've got some specific reason not to.)

Upvotes: 11

starec
starec

Reputation: 121

It's up to you. But I recommend you to have the same convention for whole project... I prefer the first way (GetMonitoredCompanies)

Upvotes: 1

Related Questions