Menu

Same Name/Different Meanings

2002-07-18
2012-10-07
  • David Dixon-Peugh

    This is a case of someone being clever with Method
    Overloading.

    There were two methods in a class (actually these two
    methods were in about 25 classes, but thats another story.)

    Each method was named *exactly* the same thing, and
    took *exactly* the same set of arguments.  But one of these
    had taken the arguments in a different order. 

    One of these methods was rather small, and the other one
    was very big.  This is a little disconcerting as the little one
    easily gets burried in the tail of the large one. 

    The big problem with it however, is that they did two
    different things.  (I would say completely, but that depends
    on your definition of completely.)

    The little method, iterated over an array stored in the class,
    and called the big one.  I believe the information from the
    array was passed as a side effect, because it certainly
    wasn't a parameter.

    Needless to say, this type of construct can only serve to
    confuse those who come behind, and should probably be
    gotten rid of.  (Of course the person who did this, probably
    thought they were being exceedingly clever.)

     
    • David Dixon-Peugh

      Detecting that two methods have the same name is quite
      easy.  Unfortunately, having two methods have the same
      name is also quite common and a good way of doing things.

      Either one has more parameters than the other, or one does
      a conversion on an easier to get at type.  For example, this
      is good:

      public InputStream getStuff( URL url ) {
        // get stuff
      }

      public InputStream getStuff( String url ) {
        return getStuff( new URL( url ));
      }

      This is also good:

      public void doSomething( Chicken buck,
                                           Dog bow,
                                           Duck quack) {
        // doSomething
      }

      public void doSomething( Chicken buck, Dog bow ) {
        doSomething( buck, bow, null );
      }

      -------------------------------------

      Perhaps what we can look for, is identical method names,
      with the same set of parameters.  This is most likely bad:

      public void doSomething( Chicken buck,
                                           Dog bow,
                                           Duck quack)
      {
        // Do Something
      }

      public void doSomething( Chicken buck,
                                           Duck quack,
                                           Dog bow )
      {
        // Do something different, or even the same, if you
        // believe in Cut & Paste
      }

       

Log in to post a comment.