Response to "Thoughts on Haml"

by Melvin Ram

James Britt wrote up a blog post talking about his experience with HAML, the alternative to ERB. His basic position (from what I gathered) was that it’s for noobies and that no one has explained to him why or how HAML makes real world code better… either than saying “wait, it’ll start looking beautiful soon enough and you won’t go back.”  This is my detailed, somewhat-reasoned response.

I like HAML because it’s clean and easy to read. Cleaner & easier to read than ERb… and yes, I’ll back that up with code:

http://pastie.org/private/n8ipfrkxlremo4jwtxiww

Converting the ERB file to HAML took less than 4 minutes, mostly hitting the delete key.

A 39 line code becomes 31 lines… and that’s not even the big win. Now if you don’t think the HAML code chuck is visually easier to digest than the ERB version, that’s fine. Stay with ERB.

I find HAML code is easier to maintain & write… and it has nothing to do with a lack of knowledge of html or anything else.

How is it easier to write and maintain?

First, there is the overt clarity when it comes to sass (css) & haml code.

## haml file
.container
  .header
    %h1 Site Name
    %ul
      %li= link_to ...
      %li= link_to ...
      %li= link_to ...
  .content
    stuff
  .footer
## sass (css) file for this

.container
  background-color: black

  .header
    background-color: white

    h1
      font-family: Funky
      font-size: 3em
    ul
      li
        a:link, a:visited
        a:hover
  .content
  .footer

Do you see the pairing? I LOVE that! I find it clean & easy to read.

Secondly, it’s very ruby like.

This:

<% if @subscription.state == 'trial' %>
  <label>Trial expiration date:</label>
  <%= @subscription.next_renewal_at.to_s(:long_day) %>

<% else %>
  <% if @subscription.amount > 0 %>
    <label>Next charge date:</label>
    <%= @subscription.next_renewal_at.to_s(:long_day) %>
    <br/>
    <label>Next charge amount:</label>
    <%= number_to_currency @subscription.amount %>
    <br/>
    <label>Current payment method:</label>
    <%= @subscription.paypal? ? 'PayPal' : "#{@subscription.card_number} / #{@subscription.card_expiration}" %>
  <% end %>
<% end %>

becomes:

- if @subscription.state == 'trial'
  %label Trial expiration date:
  = @subscription.next_renewal_at.to_s(:long_day)

- else
  - if @subscription.amount > 0
    %label Next charge date:
    = @subscription.next_renewal_at.to_s(:long_day)
    %br/
    %label Next charge amount:
    = number_to_currency @subscription.amount
    %br/
    %label Current payment method:
    = @subscription.paypal? ? 'PayPal' : "#{@subscription.card_number} / #{@subscription.card_expiration}"

Which looks & feels more like ruby? For me, the choice is clearly the second one. It literally removes anything that doesn’t communicate what the code should do.

Anyway, to each his own.

{ 1 trackback }

A study of Sass (meta-language on top of CSS) | Caldeas Blog
June 15, 2009 at 2:29 am

{ 5 comments… read them below or add one }

Boris May 6, 2009 at 11:37 pm

This is a nice analysis.

By the way, for whatever reason, James Britt's original post is now backdated by a day, breaking any links to it:

The new link:

http://www.jamesbritt.com/2009/4/29/thoughts-on...

melvinram May 6, 2009 at 11:49 pm

@Boris – Thanks for the kudos and the heads up on the changed link. I'll update the post.

Nils Ås June 8, 2009 at 7:11 am

I still think ERB looks better. Perfection is achieved, not when there's nothing left to add, but when there's nothing left to take away.

melvinram July 9, 2009 at 7:35 pm

Exactly. With ERB, there is a lot left tot take away. :) Anyway: “To each, his own.”

jimbob December 15, 2009 at 4:26 am

ERB code is utter garbage. HAML is clean, tight, compact. Just look at the rendered HTML and tell me ERB is any good.

Leave a Comment