The Advancing PHP Developer Part 3: Refactoring


3. Refactoring

Code refactoring is a programming technique that involves rewriting code in a way that improves its readability or simplifies its structure without changing its results. Thus refactoring is mainly about improving code maintainability. Code smells is a term coined by Kent Beck, and refers to certain attributes of written code that suggest it should be refactored.

There is another advantage to refactoring beyond maintainability, and it relates to development decisions. In software development, there is a tendency to over-anticipate what would be needed in the future. Functions with many parameters get written, classes get many methods to accommodate possible use-cases and projects get classes that encapsulate speculative possible future needs. A lot of code is written on the basis of what could be possibly needed next.

The reason this is happening is that programmers are reluctant to touch code that has already been written and is working properly. By using refactoring, programmers can maintain a better balance between operational and flexible code, writing code that works now and refactor when its needed. This can save a lot of development time, as most of the anticipated use-cases never come to pass (as is the case when trying to predict the future). I am not suggesting avoiding planning ahead when deciding on architecture, however when implementing it’s often better to settle on ‘good enough’ rather than ‘perfect’ (which might be unachievable).

Extra reference material:
Wikipedia: Refactoring
Refactoring homepage
Suggested reading: Refactoring, Improving the Design of Existing Code
Refactoring PHP code

Previously: Object Orientation, Next up: Testing

To know when the next article is published, please subscribe to new articles using your Email below or follow me on Twitter.

Subscribe to Blog via Email

Enter your email address to receive notification about new posts.

  • Hire PHP Developer

    Above statement answer is 6($factor=2*3==6?2*3:3*2; in this $factor will be filled with 6). This statement really saves too much development time. But it is complex to understand if expressed with long and tough formula or operations rather then simple multiplication or addition operations. It is less effective when we need nested decision making. Am I right???

  • php developers

    Nice piece. I was checking constantly this blog and I’m impressed! Very helpful information specially the last part. I care for such information a lot. I was looking for this particular information for a long time. Thank you and good luck.