Review: Refactoring Legacy Applications Using CakePHP
The Setup
Shortly after Chris Hartjes released his new book Refactoring Legacy Applications Using CakePHP he contacted me and asked if I'd be willing to review it. I jumped at the chance and Chris emailed me the DRM free PDF. After posting it to The PirateBay, I settled in and gave it a read.
The Details
The book is 61 pages and costs $10 for the PDF or slightly less than $20 for the print version. This isn't a book for CakePHP beginners. The ideal reader has good working knowledge of PHP and a basic understanding of CakePHP. Chris gives a quick overview of associations, behaviors, and routing, but it's more of a refresher then a in depth lesson. If you're reading and hit a concept you don't understand take a break from the book and visit The Cookbook.
The Review
In the last CakePHP digest I gave a mini review where I said:
I think by calling it "Refactoring Legacy Applications Using CakePHP" Chris limits his audience, as the book is really about adapting "spaghetti" style PHP code to CakePHP.
What I mean is don't get hung up on "oh, I don't have an application I want to refactor, so this book probably isn't for me." Really the book is about refactoring your brain (which could be considered a legacy application) away from a mish-mosh coding style to CakePHP's MVC system. Think of it like one of those makeover specials they do on Oprah (uh...the ones I've heard about from other people). You don't have to actually be planning to give yourself a makeover to enjoy the show and learn something.
The Before Code
Chris uses a baseball game admin tool for the code that he is refactoring. The before code is pretty much what you'd expect - an orgy of PHP, HTML and SQL all intertwined in a giant sticky mess. Some of the example blocks are pretty long, spanning several pages. It's up to you how much time you want to devote to understanding the old code. Chris does a good job of describing what the code does, but you'd probably get more value by taking the time to carefully read and try to understand what's going in.
The After Code
Each of the sections generally follows the same pattern. Some cracked out PHP code is shown, then Chris explains how to break it up to fit into CakePHP. You'd think that would get redundant, but each section tosses in new elements, building on what you've already learned. There is an emphasis on the fat model/skinny controller approach that all the popular kids are using. If you're not already on the FMSC bandwagon now is the time. In like 6 months everyone will be all SMFC and you'll still be blogging about FMSC and we'll all make fun of you.
I really like this style of learning over the "tutorial" approach seen in other CakePHP books. By looking at the legacy PHP code and seeing how Chris turns it into CakePHP your not just following some cookie cutter blog tutorial. You're forced to think about what's happening and can apply that reasoning to your own code. You do more learning and less memorizing.
The Part Where I Say Negative Things and Wax Nostalgically
The refactoring in the book went smoothly...a little too smoothly. I would have liked to see more crazy, dailyWTF style shit. For instance Chris admits that the database is shared with another CakePHP app, so it already follows Cake's conventions pretty closely. Cake has a reputation for being too rigid, but really you can make the models fit any database structure. I would have liked to seen a section on either porting data or adapting models to fit odd database schemas.
When I was rebuilding XplodSony.com (with Cake 1.1 no less) it was going to share the product database with SonyMETC.com. SonyMETC is written in WebObjects, which is a framework for those who like to program on a keyboard made of broken class, covered with tetanus. Needless to say, that database fit the Cake conventions about as well as The Glove fit OJ, but thanks to some model craziness it was still still able to work. I know both of these sites kind of look like shit now, but don't blame me. I broke up with them two years ago and they've been on a downward spiral ever since. I have that affect.
The other section I would have liked to see is something on partially refactoring an app. Sometimes it's just not possible to rebuild the whole thing at once, so you want the legacy app and the Cake app to run side-by-side. We're doing this at my current job. Most of our app is a V2 of an old PHP app that is now running on Cake. However we knew that there was no way we'd have time to build the reporting system before launch, so we took the legacy reporting system and wedged it into the Cake app. This allowed us to launch on schedule and take our time building the new reporting system right. And damn straight we took our time. A year later and we're almost half way done!
The End
So is the book worth your $10? Really $10 isn't that much, especially for what you're getting. I know the economy sucks right now and you're probably trying to be smart about your money. Here's the way I see it. In a year either the economy will be recovering/ed and everyone will be hiring CakePHP devs at which point that $10 will look like nothing next to the $250/hr we Cake devs pull down. OR society will have collapsed into a post-apocalyptic lawless cluster frak (with zombies) and money won't have any meaning. Either way you win, right?
PS
Obviously, the Pirate Bay thing was a joke. I would never do that. Chris worked very hard on this project and it shows. Don't copy that floppy!

3 Comments
Now I'm just dreaming about your Baker brains... Oh brother...