Don't Be Clever

Ryan Davis said his theme for 2007 is shaping up to be "don't be clever." However, it can be fun when pair programming to mess with your pair by writing some quick, clever code when he/she steps away. I was pairing on some code that needed reference to the days of the week, so I dropped this in:

DAYS_OF_THE_WEEK =
  (0..6).inject([]) { |a,i| [(Time.now + (i * 86400)).strftime("%A").downcase, *a] }

After my pair came back and reacted how I expected him to, I replaced it with the better version:

DAYS_OF_THE_WEEK = %w[monday tuesday wednesday thursday friday saturday sunday]

And, to flog them:

$ flog clever_days.rb 
Total score = 10.3653569644272

main#none: (10.4)
     3.3: assignment
     2.0: inject
     1.7: now
     1.7: *
     1.5: +
     1.3: strftime
     1.1: downcase
     1.0: branch
     0.5: lit_fixnum

And the second version:

$ flog better_days.rb 
Total score = 0

The flog score sums up the cost of cleverness well. For more on this theme, I recommend watching Ryan's RubyConf presentation.