Using a ParamArray, but requiring at least one par

2019-03-29 22:12发布

问题:

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.

回答1:

Another option to consider would be

Module ParamArrayTest
    Sub ShowThings(ParamArray MyThings() As Integer)
        For Each thing As Integer In MyThings
            Debug.Print("{0}", thing)
        Next
    End Sub

    ' Don't try to call without parameters:
    <Obsolete("Must have at least one parameter", True)> Sub ShowThings()
        Throw New ArgumentException("Must specify at least one parameter")
    End Sub

    Sub Test()
        ShowThings(3, 4, 5)
        ShowThings()
    End Sub
End Module

The <Obsolete()> tag with a second parameter of True informs the compiler that attempting to use the marked method should result in a compilation error. Since the method in question would be used when, and only when, an attempt is made to invoke the method without any parameters, it would cause an error only at such times. Note that method will not be used if an attempt is made to pass the method a zero-element array of Integer; in that case, the normal ParamArray form would be used.



回答2:

I think that the option that you mentioned is the best option. Using clearer names for your parameters will make it less confusing:

Public Sub Subscribe(mainChannel As ChannelType, ParamArray otherChannels() As ChannelType)

The other option is to enforce it at run-time, but as you said it wouldn't fail as fast:

Public Sub Subscribe(ParamArray channels() As ChannelType)
    If channels.Count = 0 then
        Throw new InvalidOperationException("At least one channel is needed")
    End If
End Sub