Tuesday, 3 February 2015

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 {}


No comments:

Post a Comment

Jamstack Evolution: Building Faster and More Secure Websites in 2025

As a Principal Engineer with extensive experience in frontend development, I have witnessed the transformative impact of Jamstack since its ...