views:

363

answers:

6

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?

+1  A: 

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

starec
+9  A: 

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.)

Chris
A: 

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.

Mat
A: 

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.

Wilka
+1  A: 

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.

RedFilter
A: 

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.

Alex