exercism.io

As iron sharpens iron, so one person sharpens another.
-Proverbs 27:17

Back when I was first learning to use a chef’s knife, I would pick up a five pound bag of potatoes on my way home and practice knife work for an couple hours before going to bed. The potatoes inevitably went in the trash – I never had any intent of eating them.

It wasn’t until I read The Clean Coder a few years ago that I realized the value of writing code that has nothing to do with the project at hand. Since taking the developer evangelist job at Twilio, I’ve been trying to write at least a little bit of code every day, and trying to be intentional about improving my craft through practice and study.

To that end, for the last couple weeks I’ve been doing Katrina Owen’s exercism.io. The gist is that you get a set of tests, make them pass, then submit the solution to the community for “nitpicking.” Based on the feedback and seeing others’ solutions to the same problem, you’re encouraged to submit revisions.

I’m writing this post because I’m tickled pink at the difference between my first solution to a problem:

class Hamming

  def self.compute(a, b)

    hamming = 0
    (0..shortest_length(a,b) - 1).each do |i|
      hamming += 1 unless a[i] == b[i]
    end

    hamming
  end

  private

  def self.shortest_length(a,b)
    a.size < b.size ? a.size : b.size
  end

end

And my fifth:

class Hamming

  def self.compute(a, b)
    [a, b].min.size.times.count { |i| a[i] != b[i] }
  end

end

Shoutout to Alex Clark for his iterative feedback. Comments with this mix of encouragement and instruction are so rare:

Damn near perfect. Now it’s time to start looking at the enumerable methods. Originally you had an each; that’s a good sign that there is a perfect enumerable method for what you want to do. See if you can get rid of the hamming variable.

Perfect. One last question, how do you feel about shortest_length taking an array? I’m not sure how I feel about it myself; but it is one less argument and therefore more flexible.

Now, the single line implementation probably isn’t the “most correct” version. I’ve been reading Sandy Metz’s Practical Object-Oriented Design in Ruby and she would argue that this violates Single Responsibility Principle… and she would be right.

But exercism.io isn’t about finding the “most correct” to solve a problem. It’s about discovering different ways to solve a problem and discussing the merit of each without fear of striking up a pedantic flame war. It’s about being confident enough to say “There might be a better way to do this… what do you think?” and being rewarded for your humility.

It’s about sharpening one another.