Thoughts on HAML
Feb 10, 2010 07:54

Over the last year or so I've been doing all of my new Rails development in HAML rather than in RHTML.

I've spent a lot of my professional life wrestling with 'helpful' frameworks that are supposed to make building web pages easier, but end up making the process more difficult (Lotus Domino, Websphere Application Developer, and Struts Taglibs, I'm looking at you!). These other frameworks were designed to help people who don't really know HTML or CSS, so they have lots of 'helpful' features to keep you from having to worry about what's happening underneath - features that end up getting in the way when you try to do anything new or different from their assumptions.

HAML makes a different assumption: you already know how to write HTML and as a matter of fact you don't want to be abstracted from implementation details - but you would like to work with something that's cleaner and more structured.

(Note that everything I'm saying here also applies to HAML's CSS cousin, SASS - but I'm just going to talk about HAML here to keep things focused).

Going from HTML to HAML is like going from Java to Ruby: there's so much less code to worry about and everything is so much cleaner. The logic and structure of the page is more obvious and it's easier to figure out what's going on. When the trees are smaller, it's easier to see the forest.

A good framework will make doing things the right way an aesthetic decision. I've found this in Ruby as well: because most of the code is terse and elegant, the awkward parts stick out more, and call out more to be fixed.

In fact, what I like most about HAML is the way that it makes bad habits ugly.

You're more likely to care about a piece of dirt in a beautiful clean room than in a filthy basement. Likewise, something awkward in HAML sticks out a lot more than it would in, say, PHP. If your desk or your kitchen or your code or your web page is already messy, an extra bit of mess isn't a big deal. If the context is already clean and elegant, there's much more incentive to keep things that way.

In the rush of deadline-based development, I often find myself taking messy short-cuts: one-off custom CSS attributes, big chunks of JavaScript mixed in with the page content, too much passthrough Ruby, or even using tables for certain kinds of layout. These hacks disappear into the muddle of regular HTML - but they stick out in a HAML page like a coffee stain on a white kitchen counter.

It's not a surprise that HAML comes from the world of Ruby and Rails, with its history of opinionated design assumptions and focus on powerful elegance and simplicity. Try it if you haven't yet - it'll bring aesthetics and clarity back to your HTML, and help you keep your good habits.

Next:
We're all LUsers
Feb 12, 2010 12:25