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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With