Why should I declare a method parameter using the base type?

I have declared an easy way to try converting input from a textbox to int like so:

int TryConvertTextToInt(TextBox box)
{
    //do try catch
}

      

My IDE (SharpDevelop) is trying to give me some refactoring guidelines; in particular, that the box parameter can be declared as a base type (the two options the TextBoxBase and Control tool gives me). I know I won't be using this method with anything other than a TextBox, and if I change my mind along the way, the type of the parameter being specific will remind me that this method might need to be tweaked a bit to accommodate more wide range of inputs. I don't see the value when changing the type right now, as I am not expecting the latter and the project is small.

Is there a reason I want to do this that I'm missing, or is the IDE just too useful?

+3


source to share


2 answers


In your particular case, it probably doesn't really matter. Given what you are transmitting TextBox

, you probably never want to transmit anything but TextBox

.

However, more generally, it is often best to have the most basic parameter types and the most specific return types for methods. The idea is that, as your IDE suggests, allow the method to be used in all sorts of places later down the road.

As an example, we can look at the classic collection case. Many developers will write code that takes List<T>

as a parameter and then executes foreach

through that. It's great if they ever deal with List<T>

, but if they want to, down the road, turn around to include some LINQ expressions in there, all of a sudden they are dealing with IEnumerable<T>

. Nothing in this loop foreach

will require List<T>

, but since they didn't bother to use the base class (or the interface in that case, which is often better), they now have to either change the method signature - an immutable change, but still should never be nice - or add.ToList()

to their LINQ, which violates many of the benefits of using LINQ (as they suddenly have to iterate over the collection, perhaps less than three times).



You always want to make sure that you accept a minimal class or interface that still provides the members you need. Of course, don't be too conservative: if there is a correct path and a generalized way, choose the right way, but if they match, it is best to generalize.

But again, in your case, I don't think this is very important. It is likely that you will want to use other members that it TextBox

provides than you would like to pass to other descendants TextBoxBase

or Control

to your method. It really depends on your application. Review what you have and what you need, then build your response to this warning. It sounds to me like you are rightfully happy to leave him, and this is what you should do. But for future reference this is an idea.

+4


source


the specific type of parameter will remind me that the method may need to be slightly modified to provide a wider range of inputs



But it didn’t need to be changed to accommodate a wider range of inputs, so this "reminder" would be misleading. Your IDE found that it could already host them because you haven't done anything TextBox-specific in your implementation. Only if you plan on making it actually TextBox specific in the future if you ignore the advice.

+1


source







All Articles