The answer is 42.

How to tackle boilerplate coding in Rails

Boilerplate is any text that is or can be reused in new contexts or applications without being changed much from the original. – Wikipedia.

Even if one of Rails’ mantra is DRY (Don’t Repeat Yourself), we often find ourselves repeating stuff, if not in the same project, at least across multiple projects. How many times have you found yourself configuring the ExceptionNotifier plugin, or SMTP server settings, or capistrano scripts in exactly the same or at least very similar ways? Its time to go meta and stop repeating even across projects. Here are some tips:

  • Rails 2.3 added support for Rails templates. The idea is instead of generating your projects the usual way:
rails -d mysql sample_project

You do:

rails -d mysql sample_project --template=mytemplate.rb

where mytemplate.rb is a ruby file which specifies commands to run during project generation. You can specify things like, which gems and plugins to install, generators to run, define some basic routes, etc. More importantly, since template file is a script, you can prompt the user for input before installing things. For example, you may want to include jquery in some projects but not others.

Here is a nice collection of Rails templates: https://github.com/jm/rails-templates

  • Before, Rails template was born, Jim Neath created Bort which is a base Rails application. It has some basic plugins installed and Restful authentication plugin generator ran. You can now of course replace Bort with a template (see here: http://m.onkey.org/rails-templates). However, the idea of Bort is quite powerful. You can create a base application that you always use and then add a template script using which you can customize things that change from project to project.
  • Even while writing your day to day code, you often find yourself writing some pattern many time. Any good editor provides ways to create snippets. For Emacs use yasnippet, Vim has snipMate. For example, on Emacs you can set it up so that in a view if you type:
%rp<TAB>

yasnippet will automatically expand it to:

<%= render(:partial => "" ) %>

and place the cursor between the double-quotes. Similarly, there are numerous shortcuts already defined and you can of course define your own.

  • A Rails-y way to reduce effort of writing similar code is to create a generator or a plugin for it. For example, if you find your adding the same types of models to every project (e.g. a settings table) you can create a generator for it. Or, you can even create a script which does not really generate any code but does some config changes in the project.

In summary, start by learning your editor, shortcuts and snippets; adopt a good template file for generating your projects and improve it gradually. Also, watch your steps as you move from project to project and be on the lookout for chances of creating a generator, a script or a plugin. As they say, one of the virtues of being a good programmer is being lazy.

Post a comment