I wrote some PHP today. I didn’t mean to.
All I wanted was a way to integrate strategically tagged pictures from flickr into a website. I couldn’t find any ready-made solutions to do what I wanted (making sure the page blends in with the rest of the site is the number one requirement (aside from actually pulling in the pictures)), so I had to write it myself. And while my [host](http://dreamhost.com) is certainly capable of running ruby or python…I couldn’t figure out how to do it without running rails or something. With PHP, on the other hand, it’s as easy as creating a file with a .php extension. So PHP was definitely the path of least resistance.
I haven’t really done much with PHP. I used it in college once, but that project involved staying up for forty-eight hours while banging out bad code in the computer lab. I can’t really remember much from that time other than prefixing variables with dollar signs. Which is probably for the best. (Despite handing the project in a couple of weeks late, I got an A by hanging out with the professor for a few hours. It was that kind of class.)
So I banged out a web page to do what I wanted. And I realized just how hard it is to write good code in PHP. I’m sure it’s possible, but I certainly haven’t figured it out yet.
After just a couple of hours, I already had a mostly unmaintainable mess on my hands. There are global functions and it’s basically impossible to tell where my logic ends and my markup begins. I’ve already refactored things out once, but things have already deteriorated.
Until now, I’ve considered myself a fairly disciplined programmer. I’ve developed pretty good instincts for setting up class hierarchies, making sure that everything I write only has one responsibility, avoiding code smells, and things like that. Yet somehow, over the course of hacking out this little project, I’ve managed to churn out something more expected from a first year programming student. I’d like to think I did it a little faster than that hypothetical first-year would have, but still.
I think it’s because I don’t actually know PHP. In most languages, that would mean that I just couldn’t have done anything and would have had to learn the language first. But PHP has such a large (if somewhat disorganized) library and there are so many copy-and-paste examples on the Internet that not knowing the language wasn’t actually a barrier at all. PHP let me get it done anyway.
So, score one for immediate pragmatism but minus a thousand for technical debt, you know?
I’m sure it’s possible to write good, maintainable software if you know the language well. But I suspect it’s hard. It’s so easy to write crappy PHP code (and I know because I just did it) that I can’t really imagine it being any easier to write good code. I imagine, for the PHP programmer, it’s a constant struggle to do things the easy way and just get it done versus spending a few extra minutes to do it the right way.
But I’m glad I don’t have to do it.
So, I salute all of the hard working, smart PHP programmers who are writing solid code against fantastic odds. Better you than me.
After watching two fantastic [PDC](http://channel9.msdn.com/pdc2008/) videos, [Deep Dive: Dynamic Languages in Microsoft .NET](http://channel9.msdn.com/pdc2008/TL10/) and [IronRuby: The Right Language for the Right Job](http://channel9.msdn.com/pdc2008/TL44/), I started doing some serious thinking about dynamic languages and .NET. At work (where we use VB.Net), we tend to throw around several different data types that are essentially duck-typed: objects with key/value collections that if it has a certain set of keys, it’s of type A and if it has another set of keys, it’s of type B.
What would happen if we combined these objects with the capabilities of a dynamic language?
I have no idea. With the exception of Objective-C, I don’t have much experience with dynamic languages. And Objective-C, sitting on top of C like it does, is hardly a candidate for running on the [DLR](http://en.wikipedia.org/wiki/Dynamic_Language_Runtime). And it’s hardly as expressive as something like Python or Ruby, which are conveniently the first languages available on the DLR.
So before I could start experimenting with our stuff and dynamic languages, I had to actually learn a dynamic language. I picked ruby, mostly by coin flip. I’m not starting with IronRuby right away. I find I’m always happier when I can manage complexity and it seemed that adding a not-quite-finished runtime to the mix could cause me trouble that I wouldn’t be prepared to fix. So I installed the “official” Ruby package for Windows and got to work.
I started out by implementing [Conway’s Game of Life](http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life) in Ruby. Over the course of an afternoon, I was able to whittle it down to 91 lines by swapping out the procedural statements I’d written for more Ruby-ish idioms. The Game of Life has such a simple set of requirements (there are just four basic rules) that I didn’t have to think about what the program had to actually do and could instead concentrate on the language I was writing it in. I think this is going to be my go-to first project when learning a new language from now on.
After that, I started implementing the first few [Project Euler](http://projecteuler.net/) problems (the first one is doable with just one line of ruby). It didn’t take long for the math to get hard enough that I was no longer concentrating on the language, though. So I stopped pretty soon into the problem set.
I’m not really sure what I’m going to do next. Possibly something like Tetris, but it’s all up in the air. But even with this limited exposure to the language, I’m starting to look at the code I’m writing day-to-day and saying “How would this be easier in Ruby?”. I think that’s pretty valuable and so far, I’m quite pleased with the language and want to get to know it better.