HAML: The Beauty of Efficiency
“Any sufficiently complicated rhtml partial contains an ad hoc, informally-specified, bug-ridden, implementation of half of Haml.”
So, what actually is Haml? According to Hampton Catlin, Haml’s inventor:
“Haml is a rejection of the status-quo in XHTML generation. Its not pure code, nor is it a text processing language. Its something different. A way to build XHTML the way that I feel is most natural to the problem domain.”
In addition, according to the Haml site, “Haml takes your gross, ugly templates and replaces them with veritable Haiku.” Let’s take a brief moment to explore what is meant by ugly templates, and what is actually considered to be veritable Haiku.
Here’s an example of an ‘ugly template’:
<div class="left column">
<div id="date"><%= print_date %></div>
<div class="right column">
<div id="email"><%= current_user.email %></div>
<div id="bio"><%= h current_user.bio %></div>
As you can see, it’s just a standard html/erb snippet. It probably doesn’t even look that ugly to you. However, and Grigsby explains this wonderfully in his article, there is not only a lot of repetition, but also a lot of unnecessary characters. Hampton believes that markup should be beautiful. And not any kind of beauty, he means the kind of beauty that makes you faster; the beauty of efficiency. So, in that spirit, the same snippet represented in Haml is as follows:
Now, that might look a little strange, but you have to admit it looks a lot slimmer. Notice the use of significant white space to remove the html closing tags, the css style syntax to describe div ids and classes, and the removal for the need for awkward
<%= %> tags. If you’re to count the number of characters used, the haml version in this example is 64% the size of the html version. This means 36% fewer characters to type, and 36% fewer characters to read. Essentially 36% less noise. Antoine de Saint-Exupery may well have been considering Haml when he said that “Perfection is achieved, not when there is nothing left to add, but when there is nothing left to take away.”
There are mixed responses to Haml. A lot of Rubyists seem to compare it with Python, purely because of the use of significant white space. However, Hampton has never written a single line of Python, and just feels that whitespace can be a very good way to represent structure in documents. He sees Haml as being much more like YAML than Ruby due to its focus on document structure and data configuration.
There is always space for new ideas, and DHH agrees: “I’m not sure it’s my flavor, but I love the willingness to think different”. The community seems to agree too as there is a lot of activity and discussion regarding Haml. Hampton jokes about the growing community:
“Reading the Haml forums is a weird experience. Its like reading a love letter to a technology.”
Hampton feels that 20 minutes is all you’ll need to fall in love with the simplicity of Haml. However, Grigsby disagrees, he thinks that 2 minutes is all it takes.
All this needs to be supported by tooling too.
If it was a 200 % reduction of code and also milked my cows, then I'd understand ... but for something like this, I'd rather go with a internal DSL solution like Markaby or Seaside's HTML building facilities.
Or is there something else I'm missing? (I'm not saying HAML is bad, just that I didn't have a light bulb moment yet).
i'm afraid ..
Re: i'm afraid ..
Since Haml actually comes with a little brother named Sass, which makes your CSS feel happy too.
And seriously, when you can refactor both your html and css with two little indentations or copy-pastes -- it makes all the difference.
I don't know the % that sass saves on your css, but if you wont take a look and give it a change -- you will never find out.
Thing is that this "forced indentation" is finding your typos and mistakes for you. Something that html/rhtml does not do. And the produced html is beautifully indented and readable right there in your fav. browser's view-source window.
It just makes you spot mistakes and move things around much more easily, because changing the indentation makes a difference. And getting used to it takes about 2-20 minutes, depends on who is counting.
> your fav. browser's view-source window.
Well, I saw that argument too... but frankly, why would I care?
Sure, if my target audience was urban geeks, age 15-25, who browse the web with telnet, then yes: HTML indentation is useful. But otherwise, I don't care what the HTML looks like.
The other issue is maintenance... the old question of whether Ruby or Rails code can be maintained 5 years in the future, when the original developer has long moved to his private island near Dubai... wouldn't the maintenance developer prefer to look at something that has a spec and that more than 30 people have heard of?
Oh well... maybe a Rails-style screencast (with a lot of "whoops"es) is necessary for HAML so even folks like me get it.
About maintainability - I maintain quite a bit of my own rhtml written less than a year ago, and what I have been doing for the last couple of months is mostly using the backspace and del keys, just before I "Save as..." haml.
BTW, thats has been the best bug fixer I had for my rhtml for these last couple of months. I managed to fix quite a few things this way. Quite frankly, I doubt that I would have discovered most of it even years from now by just looking at rhtml ...
So maintainability of rhtml itself is quite arguable. IMHO Haml helps with maintainability, and being so easy to learn -- I am sure the maintenance developer will manage.
When there is any problem, bug, question, suggestion, or anything else - the Haml mailing list is the place to go. I would like to thank Nathan Weizenbaum and Hampton Catlin for answering almost immediately and helpfully to all queries I have ever asked or suggested over there. And they usually do it in a matter of minutes.
To get Haml, all you need to do is take some extremely ugly rhtml that you are afraid of - and use the backspace and delete keys of your keyboard ... well, and space bar, for the indentation. If its extremely hairy - it may take you up to 20 minutes or more -- but in the end you will see the essence of the 'view' (of MVC fame) shine through the thick bracket and 'div' tag camouflage.
That must bring a shining smile of success on anyone's face.
So, if you use Rails, give it a serious try at least once.
what is the process?
.w( the_mindstorm )p.
Senior Software Eng.
Re: what is the process?
Daniel Bryant, Arian Adair, Michael Hendricks Jul 31, 2015
A. Falck, M. Rezaei, E. Pederson, B. Brodie Jul 31, 2015