Refactoring: An introduction for PHP programmers


In the coming year I’ll be starting a series on Refactoring. Refactoring is one of those programming ideas besides Design Patterns and Unit Testing which has made an order of magnitude improvement in my productivity. Although refactoring may not be a ‘Silver Bullet’ for your programming woes, it is a good tool to add to your mental toolbox.

Refactoring is a process of changing code to make it more understandable and structured without changing the code functionality or introducing additional bugs. Informally it is known as ‘cleaning up code’. Refactoring is based on the tenet that the written code is more important to the human reader than it is to the machine. Many programmers write code with the idea that once the program is complete and working the code will not be touched again. But this seldom happens. Features need to be added, bug issues need to be fixed. This may happen tomorrow or months from now; wherein you need to really remember what a particular function did. Comments in the code may be useful, but not much if the code written is not differentiable from the half eaten spaghetti bowl sitting beside.

Code refactoring is not to be confused with code optimization. Code optimization is a completely different ball game, where the goal is to run the program as fast as possible by using as few CPU cycles as you can. The focus during optimization is to speed up the program, if it impedes code understandability then so be it. On the contrary refactoring may at times make your program slower, albeit most of the times by a small factor. A sample code adapted from the wonderful book is shown below:

// Before refactoring 
if($receiptDate < SUMMER_START || $receiptDate > SUMMER_END)
    $charge = $quantity * $winter_rate + $winter_service_charge;
else
    $charge = $quantity * $summer_rate;
/* After refactoring. The statements in the conditionals   
    have been refactored to functions. */ 
if(notSummer($receiptDate))
    $charge = winterCharge($quantity);
else
    $charge = summerCharge($quantity);

Many people refactor the code after the programming part is complete, many refactor as they go along. The important part is to test your code after refactoring. Unit tests are an indispensable part of this. Keep this maxim in mind – Always Test After Refactoring.

I’m planning to post one refactoring pattern every week or two. Although I could condense all the patterns in a lengthy post and be done with, I will resist that temptation. I want you to savour each pattern slowly so that you could remember and understand each of them like the back of your hand. There are more than fifty patterns catalogued by Fowler and others so it will help if we take it slowly. Most people after seeing the complete list of patterns become overwhelmed and confused and only remember a small percentage of them. Many patterns I’ll be posting will be self explanatory so I’ll do away with elaborate explanation. Refactoring is a habit and like all good programming habits it transcends the shifting landscape of technologies and languages.

This site is a digital habitat of Sameer Borate, a freelance web developer working in PHP, MySQL and WordPress. I also provide web scraping services, website design and development and integration of various Open Source API's. Contact me at metapix[at]gmail.com for any new project requirements and price quotes.

14 Responses

1

Sameer’s Blog: Refactoring: An introduction for PHP programmers : WebNetiques

December 26th, 2008 at 3:18 am

[...] has posted an introduction to a topic he’ll be writing up a series on – refactoring for PHP [...]

2

Sameer’s Blog: Refactoring: An introduction for PHP programmers : Dragonfly Networks

December 27th, 2008 at 4:01 am

[...] has posted an introduction to a topic he’ll be writing up a series on – refactoring for PHP [...]

3

Php Program

December 29th, 2008 at 3:29 am

There are so many possibilities for you to consider. Php Program

4

A.J. Brown

December 30th, 2008 at 2:17 pm

Another goal of refactoring is improving code re usability. When a particular piece of logic has to be repeated, it should probably be refactored into a method.

One rule of thumb we keep at work is that if you have to copy-paste / yank a block of code, you’d better have a good reason for doing so instead of refactoring.

5

ncloud

December 30th, 2008 at 2:41 pm

The example could further be trimmed down with the ternary operator:

$charge = notSummer($receiptDate) ? winterCharge($quantity) : summerCharge($quantity);

6

Loïc Hoguin

December 30th, 2008 at 2:54 pm

@Ncloud: yes it could be trimmed down further. But refactoring doesn’t mean trimming down. Using the ternary operator instead of if/else is only a difference in style, while refactoring change the structure of the code.

7

hafizan

December 31st, 2008 at 1:08 am

if(notSummer($receiptDate)) {
$charge = charge_now(‘winterCharge’,$quantity);
} else {
$charge = charge_now(‘summerCharge’,$quantity);
}
// refactoring cleaning your code up.OOP more better

8

sandeep khokher

January 1st, 2009 at 11:43 pm

well refractoring is something like a combination of well understood ,self explaintory and well structured code

9

Daily Links | AndySowards.com :: Professional Web Design, Development, Programming, Hacks, Downloads, Math and being a Web 2.0 Hipster?

January 6th, 2009 at 6:15 pm

[...] Refactoring: An introduction for PHP programmers : CodeDiesel Introduction to PHP for Programmers! (tags: blog programming unread designpattern php refactoring) [...]

10

An Introduction to Refactoring (in PHP) | ReachCustomersOnline.com

February 19th, 2009 at 8:36 am

[...] CodeDiesel. This is a pretty good discussion and series about code refactoring. The quote above in particular [...]

11

Guild Invite

April 22nd, 2009 at 12:38 pm

Remember the rule for functions: Every function should only do _ONE_ thing. If you don’t do this already, try it, it’ll definitely change the way you code.

12

Refactoring | CodeUtopia

June 7th, 2009 at 11:15 pm

[...] Refactoring: An introduction to PHP programmers [...]

13

Twitter Trackbacks for Refactoring: An introduction for PHP programmers : CodeDiesel [codediesel.com] on Topsy.com

August 30th, 2009 at 9:11 pm

[...] Refactoring: An introduction for PHP programmers : CodeDiesel http://www.codediesel.com/refactoring/introduction-to-refactoring-in-php – view page – cached Refactoring: An introduction for PHP programmers — From the page [...]

14

Erika Rendon

December 10th, 2012 at 3:03 pm

I find myself rewriting code for clarity to other developer on the team. Thank you, this really helps.

Your thoughts

Sign up for fresh content in your email