Sunday, December 27, 2009

My dot-Autotest

Among the things that make me ga-ga for Ruby testing libraries is the ZenTest gem, which includes a super handy utility called 'autotest'. Autotest is meant to be run from the top of your project directory and it will search through your /test directory and run all test files it finds there (and report passes/failures on the console). My own development preference is writing specifications for my software using RSpec, which includes a tiny utility script called 'autospec' that enables 'autotest' to run anything it finds under the /spec directory of your project environment. As well, setting an environment variable called AUTOFEATURE=true tells autotest to search through your /features directory and run all your Cucumber features after file-system changes (this capability is added by Cucumber and not actually part of the ZenTest gem).

I have been running autospec nonstop as I tinker with a Sinatra app I have been writing. The normal behavior for autospec is that it runs all specs & features and then stops, watching the filesystem for any file changes that trigger it to run specs again. Recently, though, I noticed that the specs would start running and then go into an infinite loop of running and running and running... At first I thought that I'd updated a gem in the vendor directory of my Sinatra app that broke autospec somehow. After doing a lot of tinkering, I tracked it down to a Webrat log file that gets updated each time my Cucumber specs make it all the way through the login feature of my web app.

Autotest includes a configuration capability that lets you specify which file updates it should ignore (i.e. which files can be updated without triggering an execution of all the specs for your project). These exceptions can be declared in a .autotest file that needs to reside in the home directory of your development environment. I think I have covered all the bases for things that I want to keep from triggering my autospec runs. Here is what my file looks like:

The 'autotest/growl' require is so that I can see growl popups when my specs pass or fail. The gem for this is 'autotest-growl'.

UPDATE - 12/28/09: The 'autotest' script supports a '-v' flag which will cause it to report what file it saw changed to trigger your last test run. I only learned about this flag thanks to a comment on this original post by the great Ryan Davis.

Sunday, November 08, 2009

Calculator Kata revisited

Just as a follow-on, I went back to the Calculator Kata and applied a functional Ruby approach with the rule of no mutable state allowed. I was inspired by Tom Stuart's Thinking Functionally in Ruby talk.

This time I create a reusable Proc for scanning the expression according to the regex that extracts [left_operand, operator, right_operand] arrays. I evaluate the expression with reduce, using the evaluation of the first equation in the expression as the initial value, and then applying that as the left hand side of all other equations in the expression. The drop(1) call removes the first element of the parsed expression so that the reduce call can operate on the second and all subsequent equations in the expression. Since the initial value passed to reduce is the evaluation of the first equation, the memo passed to all evaluations in the reduce block is the last evaluated result.

The above code passes the same spec as I included in the previous post

Monday, October 26, 2009

Strange Passions Confusion

A funny thing happened to me on Friday. I was scheduled to present at one of the Strange Passions events for the first Strange Loop conference. Earlier in the morning, I practiced a code kata I was planning on showing when it was my turn to present. The kata was an exercise I use when I do tech interviews of prospective candidates for developer positions at the consulting firm where I work. It calls for building a calculator that can parse a string expression and return the result of the expression as a string. This is an exercise that I have used for years, one that I have done many times in Java as well as in Ruby. The problem is ideal for observing test-first habits in programmers because it is a very easy problem for which to write tests.

When the time came for me to present, I gave a few minutes of introduction to the exercise and dove in. The first test took less than a minute to write, and I switched over to writing just enough to pass the first test. And then I froze. My mind went completely blank. This is an exercise that can be solved many different ways, and at that moment, in front of a decent crowd, I couldn't think of a single one. It was a totally unnerving experience. So I sat there for a minute or two, struggling to remember how I had solved this before. The audience grew impatient. I can only imagine that it was like watching a train wreck. Then, after what felt like an eternity, something unclogged in my brain and I started up again.

The next day, I thought a lot about it. I remembered that something similar happened to me on the night of my wedding. My wife and I took Tango lessons for 4 months prior to our wedding because we wanted our first dance to be a Tango. We had a routine that the dance instructor helped us work out and we practiced and practiced. On the night of the reception, the music started and I began the routine. Then, mid-way through, everything went blank. It was a two-part routine, and the lights went out in my head at the end of the first part. I panicked and led my wife through the first part of the moves we had practiced.

These lapses in memory to execute something that is almost second nature while in front of a crowd is generally thought of as stage fright. It may very well be that I have sporadic bouts of this phobia, and what happened Friday at Strange Loop was just another occurrence. I teach classes and had presented the day before at the conference, so it is not unusual for me to be presenting something in front of an audience. It was, however, disappointing to leave the crowd twiddling their thumbs while the wheels in my head spun for traction and I am unsure what to do to prevent this same episode from happening again.

I have written several variations of the calculator kata, that I present here to anyone interested in picking this up as an exercise for their own practice. The exercise is as follows: create a simple calculator that accepts a String arithmetic expression and returns the result of the expression, also as a String.

The Spec

Solution 1

This is the ugliest of the bunch. Split the expression at each space character, then add the parts of the expression into an array until there are 3 elements in the array, at which time I figure out which operator should be applied to the left and right sides of the expression, and then calculate and store the result as the left side of the expression to be evaluated next. On line 7, I am also adding a convenience method to the array to reduce some code duplication. This solution is readable but wordy.

Solution 2

This next approach greatly simplifies the strategy for calculating a binary expression. I still split on spaces and add each part of the expression string to an array, but instead of figuring out the operator for the expression, I take advantage of the operators existing as methods on the Fixnum class. This solution turns the left and right operands into integers, passing the right operand and the operator to the 'send' method of the left operand integer, and storing the result of the evaluation back in the parts array.

Solution 3

This one is a variation on Solution 2. Instead of splitting on spaces, I use a regular expression and capture groups to extract out the operands and operators for each part of the expression contained in the string. This solution still takes advantage of arithmetic operations existing as methods on the Fixnum class, so the difference from Solution 2 is primarily in the way that the parts of the expression are extracted.

Solution 4 -- Updated - 10/27/09

This final solution is defers to the Ruby interpreter to solve components of the equation. This approach can't simply rely on the Ruby interpreter to evaluate the entire expression because the arithmetic operators have different precedence order in Ruby. I use capture groups and positive lookahead (?=(\d+)) to get the three parts of the binary expression, thereby ensuring left-to-right expression evaluation (which is how I wanted the Calculator to work).

Special thanks to Michael Easter for pointing out the flaw in the original version of solution 4, which actually did just hand the expression over to the interpreter. I hadn't included a test that combined all four operators, which exposes the order precedence issue in Ruby. That has been remedied in the spec above.

Saturday, August 22, 2009

Handy RubyMine feature - Open Current File in Browser

I just discovered a little feature in RubyMine that I think is very cool and useful. Editing a view file causes a little overlay to appear in the corner of the editor pane showing icons for Firefox, Safari, and Opera. Clicking any of these will open the currently edited file in the browser of your choosing (assuming you are working in development mode and have a server running).

Sunday, July 26, 2009

My Code Review Criteria

As a Scrum Master, I do code reviews. Recently, someone on my team asked whether what I look for in a code review was documented anywhere. This post is my starting point for documenting what I look for. I am hopeful that someone reading this may point out things that I haven't thought of that would be good to add to the list.

Backed by automated test
This should be self explanatory. No conditional logic or otherwise observable behavior should be added to the system without an automated test that confirms the predicable flow of the code.

Reinvents the Wheel
Does the code recreate some function that exists in a library included in the code base (or perhaps something from a utility library)? The chances of this are higher if you are reviewing code written by a developer new to the team. Does the code recreate a micro-framework or design pattern for which supporting types already exist and can be reused?

Does the code contain logic duplication from elsewhere in the source file or from other files in the code base? Are there refactoring opportunities to extract code under review into a block reusable within the source file?

Maintains Harmony
Does the code under review look like it was written by the same hands as the rest of the code in the source file? In my mind, all source should read as if it was written by a single diligent developer, no matter the size of the team responsible for the system. That said, a projects' code style is an issue for teams to agree on internally and be ever vigilant for its application.

Says what it is / Says what it does
Are variables, constants, methods, and types descriptively named? Are any names inappropriately abbreviated or otherwise confusing?

Tell, don't ask
Classes and other types should accept their dependencies (through injection) rather than calling out for them (either via instantiation or other means). Accepting dependencies simplifies the ability to test code as well as change implementations of the types supplied at runtime. Does the code provide some means for external dependencies to be handed to the object? Are those external dependencies specified as interfaces or other non-concrete type declarations?

Too many notes
Are there any obvious opportunities for refactoring to simplify or reduce the size of the code under review? Does each method fit entirely within the view screen? The Single Responsibility Principle says that each type in a system should have one and only one reason to change. Are any methods, functions, or types written with multiple responsibilities?

Does it work?
Finally and most importantly, does running the system or related tests demonstrate that the code under review functions as expected?

Wednesday, February 18, 2009

Git tab completion from Cygwin

I have been a fan of Git since starting to use it regularly and am going to give a talk on it at a tech lunch for my company in April. For the last two weeks, I have been using Git as an SVN front-end (my company has a backed-up SVN repo that I am using for SCM) while developing for a Windows target platform application. So far the experience with Git from cygwin has been a bit mixed. Another developer I am working with ran into problems with Git from cygwin (as opposed to msysgit) that I was unable to reproduce. His problem prevented him from being able to do simple commits on staged files. Bummer.

The little tidbit I wanted to document here is related to getting tab-completion of git commands working from the Cygwin bash shell. There are plenty of posts that already document how to do this, but cygwin kept complaining about having extra tokens in the shell scripts I was sourcing from my .bash_profile. After a little bit of forensic googling, I saw a post explaining that Windows line feed characters inserted by the text editor I was using on my shell scripts was causing my problem and that I should run the scripts through the d2u utility. Problem solved!

So for Windows folks running git from cygwin, here's how you get the awesomeness of tab-completion for git:

  1. Download the source for the version of git you have installed from
  2. Copy the git-completion.bash file from the contrib/ directory and paste it into your home dir -- (I renamed my file .git-completion.bash).
  3. Run that file through dlu: $:> d2u .git-completion.bash
  4. Source the file from your .bash_profile (in your cygwin home directory):
    if [ -e "${HOME}/.git-completion.bash" ] ; then
    source "${HOME}"/.git-completion.bash
  5. Restart your cygwin shell and you should be have git tab completion working. I can't say enough about how helpful tab completion is for git (or how smart the shell script is - truly is teh awesome)

Saturday, January 10, 2009

Learning Ruby

I began learning the Ruby programming language by using a problem I had already solved one way and rewriting the solution in Ruby. Taking a domain that I had great experience in and fitting Ruby into it made the process of exploring the syntax and rules of the language a comfortable one. To use a sports metaphor, it gave me home field advantage.

I blogged about it. In fact, the blog entry was another Ruby learning opportunity for me. The pretty syntax highlighting for the Ruby source code I included in my blog was colored by a Ruby library (gem) called syntax. I found it helpful to create a Ruby script to take the source code I had written and generate syntax-highlighted HTML (yet another learning experience).

At some point early on, I got a great reference book and dove into it as needed. Other books followed, as well as adding *many* blogs to my blogroll. As I learned, my appreciation of the language grew and I spent time working on things that would help me write Ruby.

Over time, I wrote more Ruby code as well as more about Ruby and sought opportunities to learn Ruby by teaching it. I have also been fortunate to have a patient and knowledgeable mentor to ask questions to and learn from.

The learning stage changes eventually in to the doing stage. Much like a fusion chef, I discovered ways of mixing expertise I had in other areas with Ruby. An ingenious Ruby & Swing library (or is it a framework... you decide) called Monkeybars reignited my passion for rich-client user interfaces. I dug-in and started contributing to the project and later recorded a screencast demonstrating how to use it.

Fool that I am for the fun I have writing Ruby, I wrote a Rails website to host the screencast (as well as another I recorded for yet another Ruby/Swing library). All this is to say that learning something (like Ruby) can be a rewarding and enjoyable journey. Though I have travelled a fair distance, there are still so many new directions to go. As I write this, I stand at the beginning of a new trail. At my side I have the project I will re-write, a trusty reference book on the delivery truck and a patient mentor (who doesn't blog -- looking at you, Dave Giovannini) to hold my hand when I need it. And what do you know (I think I have seen this picture before...) there is even a fusion restaurant that makes stuff I have a feeling I will like.