PHP Refactoring: Pull Up Method

As projects grow organically it is important to continually be refactoring your code base.  One simple refactoring technique which is often overlooked is called "pull up method". This is exactly what it says on the tin - pulling a method up in the hierarchy of its chain so it can easily be reused. Let's take an example:

class vehicle { }

class car extends vehicle {
  function accelerate() {
    // code here
  }
}
class motorbike extends vehicle {
  function accelerate () {
    // code here
  }
}

We can clearly see the duplication above of the 2 accelerate methods. What if e pull these up into the vehicle parent class?  Lets see how:

class vehicle{
  function accelerate (){
    // code here 
  }
class car extends vehicle {}
class motorbike extends vehicle {}


Comments

Popular posts from this blog

Navigating the Jungle of Web Traffic: A Technical Team Lead's Guide to "I'm a Celebrity, Get Me Out of Here"

TCP Handshake over IPv6

The Vital Importance of Secure Wi-Fi Networks