7 reasons I switched back to PHP after 2 years on Rails
by Derek Sivers
INTRO / BACKGROUND:
Back in January 2005, I announced on the O'Reilly blog that I was going to completely scrap over 100,000 lines of messy PHP code in my existing CD Baby (cdbaby.com) website, and rewrite the entire thing in Rails, from scratch.
I hired one of the best Rails programmers in the world (Jeremy Kemper aka bitsweat), and we set off on this huge task with intensity. The first few months showed good progress, and Jeremy could not have been more amazing, twisting the deep inner guts of Rails to make it do things it was never intended to do.
But at every step, it seemed our needs clashed with Rails' preferences. (Like trying to turn a train into a boat. It's do-able with a lot of glue. But it's damn hard. And certainly makes you ask why you're really doing this.)
Two years (!) later, after various setbacks, we were less than halfway done.* (To be fair to Jeremy's mad skillz: many setbacks were because of tech emergencies that pulled our attention to other internal projects that were not the rewrite itself.) The entire music distribution world had changed, and we were still working on the same goddamn rewrite. I said fuckit, and we abandoned the Rails rewrite. Jeremy took a job with 37 Signals, and that was that.
I didn't abandon the rewrite IDEA, though. I just asked myself one important question:
"Is there anything Rails can do, that PHP CAN'T do?"
The answer is no.
I threw away 2 years of Rails code, and opened a new empty Subversion respository.
Then in a mere TWO MONTHS, by myself, not even telling anyone I was doing this, using nothing but vi, and no frameworks, I rewrote CD Baby from scratch in PHP. Done! Launched! And it works amazingly well.
It's the most beautiful PHP I've ever written, all wonderfully MVC and DRY, and and I owe it all to Rails.
Inspired by Rails:
*- all logic is coming from the models, one per database table, like Martin Fowler's Active Record pattern.
*- no requires or includes needed, thanks to __autoload.
*- real MVC separation: controllers have no HTML or business-logic, and only use REST-approved HTTP. (GET is only get. Any destructive actions require POST.)
*- all HTML coming from a cute and powerful templating system I whipped up in 80 lines, all multi-lingual and caching and everything
*- ... and much more. In only 12,000 lines of code, including HTML templates. (Down from 90,000, before.)
Though I'm not saying other people should do what I've done, I thought I should share my reasons and lessons-learned, here:
SEVEN REASONS I SWITCHED BACK TO PHP AFTER 2 YEARS ON RAILS:
#1 - "IS THERE ANYTHING RAILS/RUBY CAN DO THAT PHP CAN'T DO? ... (thinking)... NO."
For 2 years, I thought Rails is genius, PHP is shit. Rails is powerful, PHP is crap.
I was nearly killing my company in the name of blindly insisting Rails was the answer to all questions, timeframes be damned.
But when I took a real emotionless non-prejudiced look at it, I realized the language didn't matter that much.
Ruby is prettier. Rails has nice shortcuts. But no big shortcuts I can't code-up myself in a day if needed.
Looked at from a real practical point of view, I could do anything in PHP, and there were many business reasons to do so.
#2 - OUR ENTIRE COMPANY'S STUFF WAS IN PHP: DON'T UNDERESTIMATE INTEGRATION
By the old plan (ditching all PHP and doing it all in Rails), there was going to be this One Big Day, where our entire Intranet, Storefront, Members' Login Area, and dozens of cron shell scripts were ALL going to have to change. 85 employees re-trained. All customers and clients calling up furious that One Big Day, with questions about the new system.
Instead, I was able to slowly gut the ugly PHP and replace it with beautiful PHP. Launch in stages. No big re-training.
#3 - DON'T WANT WHAT I DON'T NEED
I admire the hell out of the Rails core gang that actually understand every line inside Rails itself. But I don't. And I'm sure I will never use 90% of it.
With my little self-made system, every line is only what's absolutely necessary. That makes me extremely happy and comfortable.
#4 - IT'S SMALL AND FAST
One little 2U LAMP server is serving up a ton of cdbaby.com traffic damn fast with hardly any load.
#5 - IT'S BUILT TO MY TASTES
I don't need to adapt my ways to Rails. I tell PHP exactly what I want to do, the way I want to do it, and it doesn't complain.
I was having to hack-up Rails with all kinds of plugins and mods to get it to be the multi-lingual integration to our existing 95-table database.
My new code was made just for me. The most efficient possible code to work with our exact needs.
#6 - I LOVE SQL
Speaking of tastes: tiny but important thing : I love SQL. I dream in queries. I think in tables.
I was always fighting against Rails and its migrations hiding my beloved SQL from me.
#7 - PROGRAMMING LANGUAGES ARE LIKE GIRLFRIENDS: THE NEW ONE IS BETTER BECAUSE *YOU* ARE BETTER
Rails was an amazing teacher. I loved it's "do exactly as I say" paint-by-numbers framework that taught me some great guidelines.
I love Ruby for making me really understand OOP. God, Ruby is so beautiful. I love you, Ruby.
But the main reason that any programmer learning any new language thinks the new language is SO much better than the old one is because he's a better programmer now! You look back at your old ugly PHP code, compared to your new beautiful Ruby code, and think, "God that PHP is ugly!" But don't forget you wrote that PHP years ago and are unfairly discriminating against it now.
It's not the language (entirely). It's you, dude. You're better now. Give yourself some credit.
Ok. All that being said, I'm looking forward to using Rails some day when I start a brand new project from scratch, with Rails in mind from the beginning.
But I hope that this reaches someone somewhere thinking, "God our old code is ugly. If we only threw it all away and did it all over in Rails, it'd be so much easier!"
502 Comments
| Joseph 2007-09-22 17:58:20 |
Unintentionally hilarious? |
| Danno 2007-09-22 17:59:56 |
So, uhhh, is your rewrite done? |
| William Pietri 2007-09-22 18:44:38 |
Good for you for sticking with what suits you. We sure have different tastes, but I'm glad you ultimately judged things by real-world metrics rather than religious convictions.
|
| jason 2007-09-22 18:54:21 |
Your last comments seem to re-affirm the old cliche that PHP becomes ugly.
|
2007-09-22 19:11:01 |
Your website looks like a spam site. |
| she 2007-09-22 19:26:04 |
When i read "php is better than ruby", I had to laugh...
|
| she 2007-09-22 19:29:29 |
Oh by the way, my comment was a little too aggressive.
|
| Michael Daines 2007-09-22 19:37:08 |
It seems like starting from scratch in PHP is kind of similar to starting from scratch with Ruby and erb, which is what DDH did, right? |
| Richard Hertz 2007-09-22 20:03:29 |
Useless post without a concrete illustration.
|
| Rabbit 2007-09-22 20:52:28 |
I enjoyed the article, but now I'm sad I can no longer say "CDBaby does!" when someone asks me if anyone big is using Rails.
|
| Nick D 2007-09-22 21:17:36 |
Looks like you among many out there had a poor team. Poor development environment where you were working on several projects at once and putting priorities in wrong order among several projects. I will have to say being stuck in one mental framework can be hard to overcome, especially when learning to code in a new and cleaner manner as per Ruby on Rails. But you actually could have coded so much more faster had you stuck to your guns and not given up, or not, since you prioritized among so many projects.
|
| gnufied 2007-09-22 21:30:25 |
I agree with author, but i honestly see the point of posting it here. Take it elsewhere if you can. |
2007-09-22 21:44:28 |
This is a problem with any web framework. The idea of a framework is to make decisions for you, and if that doesn't work for you then it's the wrong framework. It's not a hit against Ruby in this case, just a hit against Rails. |
| Bob 2007-09-22 22:06:58 |
coding entire websites that do nothing is exactly what rails is great at. Having been involved in framework development that had OOP inheritance layers 25 children deep and also in heavy utility development with few objects, there is no comparison.
|
| David 2007-09-22 22:07:15 |
she: "When i read "php is better than ruby", I had to laugh..."
|
| Bob 2007-09-22 22:12:51 |
And specifically to nick: I know college grads...bla bla bla
|
| Paul 2007-09-22 23:11:48 |
Do you use memcache at all for any of the sql requests? |
| Jono 2007-09-22 23:20:26 |
Yes Derek. I had quite the same experience. Tried rewriting a web app (PHP->Rails) for about 2 weeks. I gave up and started back in PHP, but the new code was a whole lot more methodical. |
| Aaron 2007-09-22 23:22:27 |
Why didn't you hire a Rails core member? Who is Jeremy Kemper? I have been programming in Ruby for 4 years now and I have never heard of him. I think you need to reconsider who you hire next time to do things the Rails way. |
| Nazgum 2007-09-22 23:36:33 |
well personally it just sounded like you picked the wrong project; rails is opinionated and expects things to work certain ways, as you noted, and if your project is written entirely different then this - guess what, rails might not be the best choice. In your case it sounds like php definitely was the better choice.
|
| Friedrich 2007-09-22 23:41:40 |
Just few things Rails != Ruby. Ruby was there long before Rails, and it was and still is one of the easier scripting languages. Howerver PHP means currently having a broader base. And a lot of experiences. I just can tell that the PHP pages I had were relatively messed up. If one really likes to know I can check the old pages (there we have used PHP, HTML (of cource;-) Apache and Mysql. I do not like MySQL for whatever reason and so I replaces it with PostgreSQL. We are also usign a Ruby based wiki but are still using PHPBB. It works has just one really big drawback. It get's spammed with subscriptions. So it seems because it is that popular the captcha was somehow cracked. This makes me angry. I would like to fix that but the code from PHPBB is not for the faint of heart. Howerver I'm not keen on check my braveness on it...
|
| Robert Aganauskas 2007-09-22 23:55:42 |
Judging from the article what you tried to do was to rewrite your old PHP code to Ruby on Rails 1 to 1. This is the wrong way to go. Rails (and any framework in general) has some constrains you have to adopt to, and you break those only if you are *absolutely* sure there is no way to accomplish what you want within those constrains, which more often that not means that you have to change your perception of the problem - not the code. |
| Cornelius 2007-09-23 00:09:54 |
Now, this is some experience I would really like to buy a book of. The language of my company is PHP and it's very unlikely that there will be a switch. I don't see a very compelling reason as well. But I have been looking for a book of somebody with a 'puristic' mind that has found a way of working with PHP. I found none, perhaps I was not looking good enough but the books were either 'PHP 101' or 'let's create a big ball of mud'. I hope you will write more about that experience you have. |
| Cornelius 2007-09-23 00:19:28 |
I was thinking... before you have the book written ;) could you post some code examples of all coding rules you imposed upon yourself? |
| Mike Zillion 2007-09-23 01:36:24 |
Gee, I'd like to see those rule examples Cornelius asked about as well.... |
| No 2007-09-23 01:46:05 |
Haha at the Rail Kiddies... |
| wellwisher 2007-09-23 01:47:47 |
I'm a little reluctant to add to the wasteland that is this post and these comments, but here goes.
|
| Derek Sivers 2007-09-23 02:17:07 |
Thanks for the funny comments.
|
| sandofsky 2007-09-23 02:21:26 |
I came on toward the end of the project, so I can't too much credit, but yellowpages.com was rewritten in Rails in four months. That's four months from the first subversion commit to deployment in production. That was with a team of five or so, learning as they went along, with the typical business-person meddling of a mega-corporation. That includes writing a service layer that interfaces with a brand new ruby FAST library. That includes load testing and realizing you need to use mongrel handlers in a few key places.
|
| noob 2007-09-23 02:26:21 |
Well in a way you are right. Trying to make Rails work the way they are not supposed to is really a hard task to accomplish. Its crazy to even go that way. Really crazy.
|
| Christoph 2007-09-23 02:40:37 |
Reading your reason #6, I see that you totally don't get what rails is. And thats pretty sad as a resumee after 2 years. |
| dalsgaard 2007-09-23 04:13:41 |
PHP is a better PHP than Ruby (and Rails) ;-) |
| sapporo 2007-09-23 04:22:45 |
I saw it coming when you wrote "Jeremy ... twisting the deep inner guts of Rails to make it do things it was never intended to do".
|
| Not a Rails Fanatic 2007-09-23 04:57:03 |
Good post Derek. Don't let the Insane Rails Posse get you down. Your approach is refreshing. Actually build exactly what you need instead of buying into that whole Rovian "it's opinionated and only works if you kiss its ass approach and if it's not working because you're doing it wrong" talking points brief.
|
| Not a Rails Fanatic 2007-09-23 04:59:50 |
> yellowpages.com
|
| Joh 2007-09-23 05:02:45 |
The problems with PHP run much deeper than just your cosing style. * PHP handles integers wrong on 64-bit platforms. * Its namespace is polluted to a point that makes working with libraries a major pain. * Its identity and type coaxing system makes it easy to produce security flaws. * Its OO-model doesn't allow inspection which makes working with ORM like Propel or eZPublish's PersistentObject a real problem of XML/YML configuration or lots of custom coding
|
| Pratik 2007-09-23 05:17:53 |
I agree with most of the things you said about PHP not being good. But, as far as your project is concerned, it failed because of *your* lack of planning and vision. Hell, anyone could have re-written that site in JSP/mod_perl/whatever in 15 months.
|
| startup multi millionaire 2007-09-23 05:22:43 |
i like your site, minimalist, pure techie site.
|
| Pippo 2007-09-23 05:29:13 |
@jason yeah, I thought that too! LOL woot a holy crap website! :/ |
| Dave 2007-09-23 05:34:12 |
Memcache? Fragment caching? Cap deployments onto a tuned set of mongrel servers? nginx front-end dispatcher?
|
| Dave 2007-09-23 05:40:34 |
Oh and I just checked out your site. What an ugly P.O.S!
|
| Pratik 2007-09-23 05:45:44 |
And for those idiotic rails evangelists who don't know a shit about rails - check http://rubyonrails.com/core and if you ever saw changelog of rails, you wouldn't be being a moron here. |
| Matt 2007-09-23 06:16:19 |
You're not alone. Although my site doesn't have the exposure yours has, I went through the exact same thing only I had been using ASP.Net. Not to pat my own back, but I'm pretty good with ASP.Net and SQL. Fortunately, it occurred to me about 4 months in. |
| Scott 2007-09-23 06:23:23 |
Wow, if you liked PHP for those reasons, you should REALLY try ASP.NET (don't worry, you can run it on Mono if you blindly-hate Microsoft like most other people on O'Reilly). |
| Lachlan 2007-09-23 06:24:02 |
It seems to be we have two types of commentors; those who understand where you are coming from (even if they disagree) and the pre-pubescent fan-boys you'd only expect to see with something like Nintendo, Sony or Halo. |
| G 2007-09-23 06:25:30 |
The one thing I've noticed working on several rails projects is this: If you're starting on a new project, fresh and clean, no existing database, you can't beat rails. However, if you're trying to migrate an existing project to rails, more specifically, have a database that rails must be laid on top of, it gets a LOT more difficult.
|
| Carl 2007-09-23 06:30:57 |
cdbaby.com is comprised of over 100k lines of sourcecode?... terrible... you certainly are one wasteful, beat-around-the-bush lengthy programmer, as the americans usually are. |
| Carl's buddy Mark 2007-09-23 06:35:54 |
If you need 100,000 lines of code to provide what cdbaby.com does and is, I wouldn't want to hire you in a lifetime. |
| GB 2007-09-23 06:43:52 |
I too love SQL, and dislike abstractions that take the power away from you.
|
| bryan 2007-09-23 07:03:44 |
I've had the exact opposite experience, but i won't turn this into a PHP vs the world thing. Our sites handle tens of thousands of users per day and we have some extremely elaborate things going on. There have been extreme challenges, as with any other language, but we have figured out good ways to handle every obstacle with Ruby. We've even built our own distributed ruby application to handle the heavy lifting pieces, which has worked great.
|
