Posts tagged refactoring

Book Review: Refactoring HTML

Despite years of progress by web standards advocates, and a significant improvement in the quality of the HTML on the web, many of us still end up grappling with outmoded, broken HTML on a regular basis. When confronted with a large site filled with broken pages it can be hard to know where to start. Elliotte Rusty Harold’s Refactoring HTML offers a step by step recipe book for migrating such sites to clean, semantic code.

Harold’s is a well known name in the XML world, and that background shows through in how he approaches the book. While a general audience will probably find useful content, the reader needs to be prepared for a series of command-line and Java-based examples. Tools like tidy are featured prominently, as is the use of regular expressions to seek out broken code to fix and, in the music-to-my-ears category, automated testing.

If you’re equipped to do so, following these steps will lead to much cleaner, more manageable sites, but I found myself wondering how many of those comfortable with command line tools and regular expressions are in the market for a book like this.

In general I suspect the key audience for this will be IT departments inside large organisations tasked with refreshing or extending an intranet. For those developers, who maybe don’t spend much of their time working with HTML and like the idea of using scripting tools similar to those in their regular workflow, this book’s worth a look. If you’re already familiar with current trends in web development, then there are probably other ways of picking up on the scattering of techniques that might be new to you.

Disclaimer: I was sent a copy of this book for review by the publisher. You can find it at amazon US, amazon UK and all sorts of other places.

Rails 2.0 : Very Soon Now!

All reports, and the evidence of the subversion commit log, says Ruby on Rails 2.0 will be with us very shortly. Apparently the announcement has been delayed until the gems have properly circulated. For those who’ve not been following, Rails 2.0 isn’t going to seem like a huge step forward as it’s mainly focussed on cleaning up existing features, and moving quite a few out of the core and into plugins.

I’ve converted quite a few projects over to be 2.0-compatible over the past few weeks and I’ve always come away feeling like my projects are cleaner as a result. It’ll be interesting to see what the benchmarks say about the performance impact of the new release.

Some of my favourite features of the new release are:

New view filenames and improved mime type support

Rather than filling up your views folders with .rhtml, .rxml, and .rjs files you’ll now be looking at filenames of the form myaction.html.erb, myaction.atom.builder, etc. If you ever found yourself working with the likes of myaction_atom.rxml, myaction_rss2.rxml, and so on, you’ll understand that the new naming system makes life simpler and your code simpler. In general, it really feels like the support for different formats that has been creeping into rails for a while now is finally coming of age with 2.0.

Pseudo-mime types

Closely related to the previous point, it’s nice to see an officially supported way to present different content to different devices just as you’d supply different formats for different requests. That’s written up well in this piece on building an iPhone UI for your Rails 2.0 application, and would have come in very handy when I was doing my work on intercepting microformats in rails input. Put simply you can define extra aliases for a given mime type, eg:

Mime::Type.register_alias "text/html", :my_extra_format

then use a before filter to identify a request as your pseudo-mime type:

  before_filter :detect_input_format
 
  def detect_input_format
    if request_matches_my_criteria?
      params[:format] = "my_extra_format"
    end
  end

and then the resulting action will render the view myaction.my_extra_format.erb (or myaction.my_extra_format.builder, etc.).

If the long-predicted explosion of the mobile web does indeed come in 2008, or more people adopt the facebook application approach, I can see this feature getting a lot of attention.

ActiveRecord Query Caching and Serializations

Lots of databases support native query caching, but it’s good to see it going in at the ActiveRecord level as it’ll sit a little closer to your app and be something you can rely on. I’ve seen a good speed up on some apps that I’ve been running on Edge.

ActiveRecord objects can now be created from XML and serialized to json much more easily, making inter-operability still easier.

And a lot more

The new initialization process is great, being able to specify :moved_permanently in redirects has cut out code I never liked, fixtures make a lot more sense, the view cleanups are great, and there’s a lot of stuff I never used which is no longer taking up memory in my processes. My experiences with Edge Rails lately mean I’m very pleased with Rails 2.0, am glad that this release is so focussed on clean-up, but I’m also looking forward to seeing what radical changes and refactoring come along once this version is out the door.

You can learn more about Rails 2.0 by checking out this post on the Riding Rails blog, the videos at railscasts.com and Ryan Daigle’s posts on Edge Rails. I’m sure a lot more will emerge over the next few weeks.

UPDATE (later that day): DHH has announced it and it’s official: Rails 2.0 is now out.