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 car extends vehicle {
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
Post a Comment