Excessive Overloading

ExcessiveOverloading - this is a CodeSmell probably like VerbSubject construction -- Fix by using MoveMethod to move the method to the method argument.

I am confused about how this balances with OnceAndOnlyOnce... say that we're wrapping OS-dependant dialog functions, and we end up with these methods... (C++ or Java, imagine "public" and/or "virtual" are in the right places.)

  class Dialog
  {
    ...
    Control getcontrol( int itemnumber );
    void setenabled( Control ctrl );
    void setenabled( int itemnumber ) { setenabled( getcontrol( itemnumber ) ); };

void setvisible( Control ctrl ); void setvisible( int itemnumber ) { setvisible( getcontrol( itemnumber ) ); };

void setactive( Control ctrl ); void setactive( int itemnumber ) { setactive( getcontrol( itemnumber ) ); }; ... // and so on for gets... }
Say that "Control" isn't a class, so we can't MoveMethod to it. Is the above overloading preferable to the caller calling getcontrol(itemnumber) all the time? It seems like there is duplicate code either way -- in caller code, or in this class.

I'd say the overloading is preferable. Better for the redundancy and extra code to be in the class rather than in the callers. Also gives you the option of changing the implementation without changing the interface. On the other hand, if all callers can use an item number or if all callers can use a Control reference, then eliminating the overloads would be a good idea. A better option might be something that allowed clients to do something like "dialog.Controls(i).setVisible()", since the attributes seem to really belong to the controls rather than to the dialog, or "dialog.setOKButtonVisible()" so that the callers don't need to know how to get at the internals of the dialog (see LawOfDemeter).


EditText of this page (last edited July 11, 2013) or FindPage with title or text search