Ruby Best Practice Patterns, by Rein Henrichs

I watched the Ruby best practice patterns presentation, given by Rein Henrichs at the Ruby Hoedown 2008. Personally, I thought it was a very long presentation (including a useless 15-minute long dive-in) to deliver a simple message. His (nevertheless important) message was that a programmer should aim to write communicative code. And the best practice patterns considerably help to reach that goal.

Here are my highlights (divided in 3 sections):

Legacy Code and Maintainability

  • your code is legacy code as soon as it is written
  • most of our time is spent maintaining that legacy code
  • software costs far more to maintain than to write
  • write code for humans, not for computers

Best Practice Patterns

  • Definition of “Best practice patterns”:

a set of techniques experts currently use to make the best decisions over and over again – Rein Henrichs

  • the best way to learn about patterns is to read through open source code, like gems

A Few of His Favorite Patterns

Pattern: Composed Method

 

Pattern: Execute Around

Example in Ruby:

 

Example in Rails (around filter):

 

Pattern: Yield and Return

Example in Rails (implementation of the will_paginate plugin):

 

Example in Rails (implementation of returning):

 

Example in (forthcoming) Ruby 1.9:

 

Pattern: Method Object

Example in Ruby:

Advertisements

Got a comment?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: