haml, an objective view
In the last few weeks, I have been introduced to haml, which is a different way to write you views in rails. My initial thoughts about it weren’t so keen, so I wanted to take a more in depth look at it to see why people are switching to it, and why, or why not, to use it.
First of, I can see why people are switching to it. It is generally felt that it is a cleaner way to look at your views. For example, this is what something simple might look like in rhtml
<div id="profile"> <div class="left column"> <div id="date"><%= print_date %></div> <div id="address"><%= current_user.address %></div> </div> <div class="right column"> <div id="email"><%= current_user.email%></div> <div id="bio"><%= current_user.bio %></div> </div> </div>
The same code, done with haml would be
#profile
.left.column
#date= print_date
#address= current_user.address
.right.column
#email= current_user.email
#bio= current_user.bio
So, what is the first thing that you notice about the difference between these two? Well, the first one looks a lot like HTML with script tags, which is pretty much what it is. The second, looks a little bit like CSS at first glance. If we take a little closer look, we also see that in the haml example, there are no closing tages. What haml uses is very similar to python in that is uses white space checking to end things.
Some people enjoy this a lot, the fact that they don’t have to explicitly end things, but this is the first thing that bothers me about haml. It could just be the type of meticulous person I am, but I want to explicitly close things, and make sure that nothing is missed when programming. If I learned from python, I am pretty sure that this wouldn’t both me as much. Being that because I learned java as my first language, I even feel weird not explicitly using a return from a function (this throw me for a loop for a while in rails). I understand that this isn’t a very solid reason to not like something, but from a strictly person preference, I do not like this, however I do see where many would.
The second thing that bothers me about haml, is that a developer might not understand right away what it is doing. Many times have I worked with other people on projects at school, and a few programming contests, and what would mess us up the most is one of us not having the knowledge base for something and the other person trying to use skills and techniques that the other person doesn’t know. When doing some larger projects, many times the programmer will get a developer to do the views for the project, and then just fill in what they need to with script tags and the such.
Yes, I understand that at first glance, a developer might be able to read haml, and understand what is going on, but if you want to use haml for the entire application, they will need to learn how to write it. This may not be a big deal to some developers, but it might be to others. Basically when it comes to learning anything new, some people can pick it up quickly, but some might not be able to. I guess this maybe a moot point, but one I wanted to bring up none the less.
So, in doing a little bit more research between the two, I looked at what other people had to say about the two. There was really only one strong argument as why not to use haml, and it was due to the fact that it has to be reparsed before rendered and that it is a performance hit. Some metrics someone ran showed it at about twice as slow. This may not be a good argument for a lot of projects, but for those where performance matters, it is a very compelling argument.
So, personally, I don’t see a fantastic reason to want to switch to haml. I see its advantages of being cleaner, but also the disadvantages as well. I guess, I should try it on an app once, and see how I like it, and then use what works best for me because honestly, I don’t want to use something that is extremely uncomfertable for me, but if I happend to like it, then it might be something I would want to use more often. Once I give it a try, I will post my thoughts on it, but with the exception of the small things I don’t like about it right now, I can’t see a reason to dismiss it before trying it.