As any developer who values his time, sanity, or amicable relations with teammates who have to maintain his code knows, one of the cardinal rules of modern programming is "Don't Repeat Yourself", or DRY. This is obviously something that should make reading code easier. Obviously, therefore, it should be applied except where the lengths you go to to not repeat yourself make your code harder for someone else to read. The idioms of some languages help this more than those of others.
I was recently browsing through a (rather poor) Ruby programming book when this hit me between the eyes. Consider this ERB example from the book.
Pretty ugly, yes? Part of that ugliness is intrinsic to ERB; which is why people are leaving it in droves.
Compare this exact translation into Haml:
= form_for @product do |f| - if @product.errors.any? .error_explanation %h2= "#{pluralize(@product.errors.count, "error")} prohibited this product from being saved:" %ul - @product.errors.full_messages.each do |msg| %li= msg .field = f.label :title %br/ = f.text_field :title .field = f.label :description %br/ = f.text_area :description .field = f.label :image_url %br/ = f.text_field :image_url .field = f.label :price %br/ = f.text_field :price .actions = f.submit
A bit easier to understand, now that we've gotten most of the clutter out of the way, yes? Now, the repeating field definitions stick out like a sore thumb; they're almost the same, but not exactly. We've got three text_fields and one text_area, so a simple loop that just plugs in values won't quite cut it, will it? How about this:
= form_for @product do |f| - if @product.errors.any? .error_explanation %h2= "#{pluralize(@product.errors.count, "error")} prohibited this product from being saved:" %ul - @product.errors.full_messages.each do |msg| %li= msg - [:title, :description, :image_url, :price] do |name| .field = eval("f.label :#{name}") %br/ = eval("f.text_field :#{name}") unless name == :description = eval("f.text_area :#{name}") if name == :description .actions = f.submit
If you're coming to Ruby from a PHP background, you've been conditioned not to use eval; there's all sorts of nastiness that could lurk there, especially when code is sloppy. (And PHP code is notorious for its sloppiness.) But in Ruby and Haml, this lets us solve a problem more eloquently than the two or three other possibilities that come to mind. Further, the idiom of the repeated-but-not-repeated text_field/text_area fragment
= eval("f.text_field :#{name}") unless name == :description = eval("f.text_area :#{name}") if name == :description
OK, but why blog about this in the first place? Well, the company I'm working with now is looking for a senior Ruby/Rails dev. Most of the CVs we're getting are from mid-level guys (if you're a Rails doyenne (i.e., female), please email!) who have done some Ruby and one or two other languages, generally PHP and Java. What I've learned, on both sides of the table, is that if you've got some experience in several languages that aren't all closely-related to each other, it's a lot easier for you to ramp up on any language you need. Any language will eventually go out of favour. Being able to code idiomatically in anything you need to, won't.
No comments:
Post a Comment