Sam Heuck

The unforeseen challenge of dabbling in a new language


I was recently surprised by the realization that my dabbling, or perhaps more appropriately bumbling, with Haskell may have actually degraded my performance as a PHP developer.

To be clear, I happen to think PHP is a decent language. It is the language I used for my first honest-to-goodness web application that was useful to someone. Liking PHP is unpopular - as one could easily discover with a bit of reading. But the fact is that PHP is often the "gateway drug" for aspiring web developers. It certainly was for me, and seven years later (during which I picked up javascript) I find myself attending a Haskell meet up.

Haskell is about as far from PHP as you can possibly get. Haskell is compiled - PHP is interpreted. Haskell is statically typed - PHP is dynamically typed. Haskell is purely functional - PHP is procedural with bolted on object-oriented features and lambdas. Haskell is lazy - PHP is... not. But the particular dichotomy causing me difficulty is mutability.

In PHP, nearly everything is mutable. Variable assignments, and their types, can change at runtime. Any bit of code can change global or local state at any time. Stating $x = 4; at the top of a function, and then later stating $x = 'purple cat'; at the bottom of a function is not only valid, but commonplace in PHP.

However, in Haskell, nothing is mutable. The compiler forbids doing such blasphemous things. This means that it expresses ideas the same way that algebra does. If you tell the compiler that x = 4 for example, x will always be 4. Forever. The = symbol means define, not assign. If the program ever tries to change the definition of x the compiler will complain.

At this point you might be wondering what the problem is. Of course these two languages are different, why would learning about one affect my ability to do useful work in the other?

I think the answer is buried deep inside a characteristic of PHP that is both its greatest strength, and its greatest weakness. As I said, PHP is a "gateway drug" to programming. It entices the bright-eyed and bushy-tailed with its gentle learning curve, and its ubiquity. All you need to get started is a simple text editor. You don't need to know what mutability even means. You don't need to know any set theory, type theory, or discrete mathematics. You don't need to concern yourself with compilers or type systems or object oriented design principles. You can get a blog up and running in a matter of minutes by following any of the ridiculous number of PHP tutorials available online.

This is a wonderful thing! So many people can start doing useful work in PHP with very little or no programming experience. And a whole lot of people have. And a whole lot of those people, myself included, turned that into a career.

But when you start to broaden your horizons as a programmer, you learn new ways of approaching problems. You learn to think differently. Languages like Haskell open your mind to concepts that you never considered before. Like mutability. And I believe this is where the surprising double edged-sword is forged.

When you program professionally, business value is always the number one pressure. If the code you are about to write does not add business value, you are wasting money. If you spend time thinking about something that you need not think about, you are wasting money. If I spend time worrying about the fact that this PHP function just reached into global state and changed some values on the object that represents the currently logged in user, just as a convenient way to add some business value, I have wasted time and therefore money.

Before I started tinkering with Haskell, I didn't think twice about grabbing the global user object and changing some values. But now, I see mutable state as something dangerous and scary! Sometimes particularly egregious violations need to be discussed at length with a teammate before I feel comfortable doing something that is perfectly acceptable in PHP. All of this extra worrying slows me down.

That said, I believe that stretching your mind by learning antithetical techniques is important for a developer's growth, and I'm a better developer for it. I just need to learn to focus my mind in a different way before sitting down in front of a PHP project.