faded picture of luke
a semi-random photo | click for the full photo gallery
click to browse photos
homepage navigation

Luke Melia

February 28, 2007

Outliving The Great Variable Shortage

Good for a chuckle and for a good point. Time Ottinger on the generally silly practice of reusing variables:

I suppose somebody forgot to clue me in to the Great Variable Shortage that is coming. I have seen people recycling variables to mean different things at different times in the program or different states of the containing object.

Next thing you know, we’ll here stories about how the old-timers had to carry their variables to work with them. “Up the call stack both ways!”

Loosely-coupled Mocks

Aaron at the Eleutian Dev Blog has an excellent post on Loosely-coupled Mocks.

That said, I do have a few ground rules for when I write tests with Rhino Mocks that sort fall into the whole one assertion per test topic and the mocks vs. stubs thing. Here they are…

The tips he covers are definitely ones that my team has learned or is in the process of (sometimes painfully) learning. It’s very easy for tests with mocks to be difficult to understand and also to yield false failures. If your tests are brittle or intimidating to work with, you’re contrary to the goal of a test suite enabling change and refactoring.

Great to see more people blogging about Rhino Mocks!

February 25, 2007

Cleaning up respond_to blocks with lambda

Whenever Trotter presents at nyc.rb, I always pick up some new ruby idea that I can put to use immediately. His most recent presentation, "Refactoring teh Rails" [sic], was no exception.

Let's start with the end result. ProjectsController#index in Tracks before applying the idea:

RUBY:
  1. def index
  2.     respond_to do |format|
  3.       format.html do
  4.         init_project_hidden_todo_counts
  5.         @page_title = "TRACKS::List Projects"
  6.         render
  7.       end
  8.       format.xml { render :xml => @projects.to_xml( :except => :user_id )  }
  9.       format.rss do
  10.         render_rss_feed_for @projects, :feed => Project.feed_options(@user),
  11.                                        :item => { :description => lambda { |p| p.summary(count_undone_todos(p)) } }
  12.       end
  13.       format.atom do
  14.         render_atom_feed_for @projects, :feed => Project.feed_options(@user),
  15.                                         :item => { :description => lambda { |p| p.summary(count_undone_todos(p)) },
  16.                                                    :author => lambda { |p| nil } }
  17.       end
  18.       format.text do
  19.         render :action => 'index_text', :layout => false, :content_type => Mime::TEXT
  20.       end
  21.     end
  22.   end

There's nothing terribly wrong with this method except for the fact that it is a little long and is difficult to read because of the nested blocks. Trotter suggested thinking of the main CRUD methods in your Rails controllers as dispatchers. I had independently reached the same conclusion -- the answer is Extract Method. But because my Ruby skills are still developing, I couldn't figure out how to do it. After the presentation, I understood it. Here's the after code:

RUBY:
  1. def index
  2.     respond_to do |format|
  3.       format.html  &render_projects_html
  4.       format.xml   { render :xml => @projects.to_xml( :except => :user_id )  }
  5.       format.rss   &render_rss_feed
  6.       format.atom  &render_atom_feed
  7.       format.text  &render_text_feed
  8.     end
  9.   end
  10.  
  11.   def render_projects_html
  12.     lambda do
  13.       init_project_hidden_todo_counts
  14.       @page_title = "TRACKS::List Projects"
  15.       render
  16.     end
  17.   end
  18.  
  19.   def render_rss_feed
  20.     lambda do
  21.       render_rss_feed_for @projects, :feed => Project.feed_options(@user),
  22.                                      :item => { :description => lambda { |p| p.summary(count_undone_todos_phrase(p)) } }
  23.     end
  24.   end
  25.  
  26.   def render_atom_feed
  27.     lambda do
  28.       render_atom_feed_for @projects, :feed => Project.feed_options(@user),
  29.                                       :item => { :description => lambda { |p| p.summary(count_undone_todos_phrase(p)) },
  30.                                                  :author => lambda { |p| nil } }
  31.     end
  32.   end
  33.  
  34.   def render_text_feed
  35.     lambda do
  36.       init_project_hidden_todo_counts(['project'])
  37.       render :action => 'index_text', :layout => false, :content_type => Mime::TEXT
  38.     end
  39.   end

The key is this lambda keyword. In Ruby (and other languages), lambda lets you define a block -- it might help to think of it as an anonymous method body. What you get back is a Proc object, which you can pass around and treat like any other reference. To get it to work as an argument to a method expecting a block, throw an ampersand on the front of the variable name. Eli Bendersky explains all about Ruby blocks and procs better than I can.

Simple but satisfying. While I was passing on the leg of lamb at dinner last night (I don't eat most animals), I couldn't help but think "lambda... yummy."

February 20, 2007

GoRuCo ticket sales open

We've opened up ticket sales for Gotham Ruby Conference. If you're planning on attending, I'd encourage you to waste no time in getting your ticket!

February 18, 2007

Ken Judy’s new blog

My colleague Ken Judy has started a fantastic blog. Ken has been an inspiration to me with regard to the ethics of software development. He's as passionate about Scrum as I am about XP. And as you'll see when you subscribe to his blog, he's a great writer:

Meaningful products can emerge from horrible process. But a way of working that tears down talented people’s desire to work is tragic. To repeatedly participate in this is to sap the world of it’s limited supply inspiration, creativity and joy. This is evil. Now that I have authority, my main goal is to avoid this evil.

Amen, brother.

Presenting at Code Camp NYC

I'll be giving a talk at Code Camp NYC II on March 3rd. The title is "Supercharging the WPF Command Pattern with Dependency Injection". Turns out that XAML and Castle IoC is a nice combo and I'll walk through an example that Saturday.

My colleagues Wendy (blog) and Oksana (no blog) are also doing two joint presentations at Code Camp. One is on pair programming and one on testing with Rhino Mocks. Wendy is a great TDDer, and if the pair of the them are half as entertaining as they are in our team room every day, those talks will be a blast.

Code Camp is at Microsoft's NYC office near Columbus Circle. Registration is free and now open.

Speaking of conference registrations, we're opening up registration for Gotham Ruby Conference this Tuesday evening. We have a limited capacity, so it's probably a good idea to buy a ticket early.

If you're one of the bilingual C#/Ruby people who are attending both of these, I'm sure we'd have a lot to chat about!

February 11, 2007

Cool ruby code

While sipping from the firehose that is the ruby-talk mailing list, I came across this sweet bit of code:

RUBY:
  1. class Integer
  2.   def factorial
  3.     return 1 if self <= 1
  4.     self * (self-1).factorial
  5.   end
  6. end
  7.  
  8. > 6.factorial
  9. > 720

This is a nice illustration of ruby's elegance. So much nicer than calling something like Math.Factorial(6).

February 2, 2007

sqlite3 memory test verbosity

While working on Tracks, I've been writing and running a lot of tests. The project has a 181 tests so far (not including Selenium tests). A while back, I switched to using a sqlite3 in-memory database for my tests in an effort to speed them up (about 15% faster for this project).

Tonight, after some planning for Gotham Ruby Conference, I queried the group about something that had been bugging me. While running my tests, I was getting messages like this:

Creating sqlite in memory database
-- create_table("contexts", {:force=>true})
   -> 0.0041s
-- add_index("contexts", ["user_id"], {:name=>"index_contexts_on_user_id"})
   -> 0.0012s

These went on for a screenful, and happened before every test run (which is a lot with autotest -rails!).

Bryan, who works at East Media, suggested that the problem might be specific to sqlite3, and sure enough it went away when I switched to mysql. After that, some targeted searching revealed a solution. It turns out that a more recent version of topfunky's memory_test_fix plugin than the one I had has a feature to quiet/silence this annoying output. Thanks to Kakutani Shintaro for contributing this!

test:
  adapter: sqlite3
  database: ":memory:"
  verbosity: silent

I upgraded and am now happy as a clam with my concise, colorful test output.

Now I just have to refactor out these Rails 1.2 deprecation warnings!

LukeMelia.com created 1999. ··· Luke Melia created 1976. ··· Live With Passion!
Luke Melia on software development freelance web development how to contact me Luke Melia, Software Developer letters and more from my travels photo gallery personal philosophy