What I used to have:
Public Sub Subscribe(channel As ChannelType)
Public Sub Subscribe(channels As IEnumerable(Of ChannelType))
The first one just calls the second one with {channel}
to convert its parameter into an array.
I decided that having to create a list of channels to pass to the method was awkward and chose to combine the two overloads into one method that takes a ParamArray
.
Public Sub Subscribe(ParamArray channels() As ChannelType)
'Usage
Subscribe(ChannelType.News)
Subscribe(ChannelType.News, ChannelType.Sports)
Subscribe() 'Oops... this is valid
What is the "best practice" here? I like the flexibility that ParamArray
gives me in just letting people pass stuff in, but it fails to help the developer "fail-faster" via compiler error feedback... that means that something like an ArgumentException
is out of the question here since people consuming this method may not be writing any unit tests. One options is the following...
Public Sub Subscribe(channel As ChannelType)
Public Sub Subscribe(channel As ChannelType, ParamArray channels() As ChannelType)
But I feel like that puts me nearly back to square one, is confusing, and requires my implementation of that method to be less straight-forward.
ParamArray
instead of having to concat a single item with theParamArray
to iterate over my arguments. – Godwit