Should you change return types in a method overload?
The design guidelines around method overloading are clear… you should only overload methods that do semantically the same thing. That is you should be able to change your code from calling one to calling another without substantive changes to the rest of your code.
Someone recently forwarded me this thread post on change the return type in an overload. I completely agree with the conclusion reached here… I don’t see how you can change the return type and keep the method semantically the same.
As someone in the thread commented, simply choose a more appropriate name for the new method.
Wrong:
Public Function MyFunc(someParam as String) as MyCustomClass
Public Function MyFunc(someDifferentParam as Type, someParam as
String) as MyCustomClass
Public Function MyFunc(someDifferentParam as Integer) as Boolean
Right:
Public Function MyFunc(someParam as String) as MyCustomClass
Public Function MyFunc(someDifferentParam as Type, someParam as
String) as MyCustomClass
Public Function IsMyFunc(someDifferentParam as Integer) as Boolean
Based on the discussion, we should likely add a guideline to spell this out clearly:
- Do not change the return type when overloading a method
Thoughts, comments?