Move Method

A method is, or will be, using or used by more features of another class than the class on which it is defined.

Create a new method with a similar body in the class it uses most. Either turn the old method into a simple delegation, or remove it altogether.

image/svg+xml Class 1 aMethod() Class 2 Class 1 Class 2 aMethod()

Additions

Marian Vittek sent an example for moving a method to a method argument.

class Project {
  Person[] participants;
}

class Person {
  int id;
  boolean participate(Project p) {
    for(int i=0; i<p.participants.length; i++) {
	  if (p.participants[i].id == id) return(true);
    }
    return(false);
  }   
}

... if (x.participate(p)) ...

After applying the move you end up with

class Project {
  Person[] participants;
  boolean participate(Person x) {
    for(int i=0; i<participants.length; i++) {
	  if (participants[i].id == x.id) return(true);
    }
    return(false);
  }   
}

class Person {
  int id;
}

... if (p.participate(x)) ...

He also points out that the part of the Move Method mechanics that reads Determine how to reference the correct target object from the source, should be replaced by Determine how to reference the correct target object from the source or from arguments of the method which is more general.