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

Record Number of Developers Adopting AI as Vibe Coding Surges

As AI adoption continues to rise across the tech industry, a record number of web developers are turning to vibe coding to build application...