Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should a 'Get' method name include the name of the parameters?

Tags:

c#

object

Apologies if this has been asked before, and it's hard to imagine it hasn't, but it doesn't have an obvious keyword association so it's hard to check.

If I have a C# class with a couple of standard methods like GetByCode(string code) and GetByCodeAndResourceType(string code, string resourceType), should I just rename them to be an overloaded Get(...) since they are distinguishable by the parameter list?

Obviously, there may be scenarios where the same method signature may be needed for different purposes but other than that I can't see the need for separate method names as long as there are comments in place for intellisense.

I'm interested to know what anyone thinks are the pros and cons.

like image 638
Ralph Lavelle Avatar asked May 30 '11 01:05

Ralph Lavelle


2 Answers

If they both do the same thing (or return the same value) but accept different inputs then it would be better to differentiate these by overloading the parameters. If there is any difference in what they do or return then I'd use different names.

Another reason for a difference in naming may be performance, or another characteristic that is not obvious (eg. one overload is a lot slower than the other). This can be spelled out in docs but providing different names brings more attention to this.

like image 181
Brian Lyttle Avatar answered Oct 13 '22 02:10

Brian Lyttle


We tend to use the word Find instead of Get and yes we use the parameter name in the method name.

As you point out, there will be cases when the parameter types do not provide a unique signature. Which is why we opted for the long names because it is consistent.

like image 21
Richard Schneider Avatar answered Oct 13 '22 02:10

Richard Schneider