Blog


Aug

You're Cuking It Wrong


Opinions on cucumber seem to be divided in the Ruby community. Here at Elabs we've been using cucumber to fantastic success on all of our projects for more than a year. At the same time Steak and projects like it seem to be gaining traction; some people are seemingly frustrated and fed up with cucumber.

So where does this gulf of experiences come from, why is cucumber loved by some and hated by others. At the risk of over-generalisation and mischaracterisation I recently came up with a theory: the cucumber detractors are not using cuke the way it was intended.

This is in fact not their fault. The entire cucumber ecosystem, and in fact even cucumber itself, encourage its misuse.

A while ago someone created an issue on the Capybara issue tracker. The interesting thing about this issue wasn't the problem itself, but rather the cucumber feature that the author presented in order to replicate the problem. This is the feature the author submitted:

Scenario: Adding a subpage
  Given I am logged in
  Given a microsite with a Home page
  When I click the Add Subpage button
  And I fill in "Gallery" for "Title" within "#document_form_container"
  And I press "Ok" within ".ui-dialog-buttonpane"
  Then I should see /Gallery/ within "#documents"

At first glance this seems reasonable. But contrast this with the following, improved version:

Scenario: Adding a subpage
  Given I am logged in
  Given a microsite with a home page
  When I press "Add subpage"
  And I fill in "Title" with "Gallery"
  And I press "Ok"
  Then I should see a document called "Gallery"

The difference isn't huge, the steps are largely the same, and there's an argument to be made for writing in a more declarative style, but there's one crucial difference: the first feature is code, the second isn't.

The argument against cucumber that's often presented is that as a programmer, plain text is unnecessary, because we can all read code. While it's true that we all can read code, I still find it beneficial to jump out of the code writing mode for describing the behaviour of the application. When you're writing features first, you don't want to be bothered with the details of how this functionality works. In this initial stage you care nothing about the implementation, about how the result is achieved. You care nothing about things like #document_form_container or .ui-dialog-buttonpane.

I believe that it's in this switching between designer mode and developer mode where cucumber, done right, really shines.

In order to evaluate the bigger picture before hacking
As a developer
I want to write my stories before writing my code

There are some secondary benefits as well. Writing truly plain text features leads to better maintainability as well, since the features are robust against code changes. Plain text is also easier to understand for new developers coming to an existing project. Probably the nicest advantage though is that over time a library of steps is built up, which can then be simply combined to describe new features.

The above feature is nicely illustrative of this anti-pattern, but it is far from the only example. In many of our cucumber suites here at Elabs, we have steps like the above, some of them were written by me. Which leads me to what's really wrong with the last three lines of the above feature. They are written using nothing else than the standard web steps generated by cucumber-rails own generator. Cucumber itself ships with steps which in my opinion encourage an anti-pattern.

Pickle my fancy

Another tool which we've experimented a bit with is Pickle, which allows you to easily generate models from your feature files. A basic example from the README:

Given a user exists
And a post exists with author: the user

Given a person: "fred" exists
And a person: "ethel" exists
And a fatherhood exists with parent: user "fred", child: user "ethel"

It actually looks fairly nice, reads quite naturally, so a first instinct might be to call this plain text. But on closer inspection, there is a whole language in there. To comprehend what these steps are doing you'd need to understand not only the domain models involved, but also the language Pickle uses to manipulate these. I'm pretty sure a non-technical person couldn't make sense of the above. This is really no different, and in fact worse, than writing actual code:

@user = User.make
Post.make(:user => @user)

@fred = Person.make(:name => 'Fred')
@ethel = Person.make(:name => 'Ethel')
Fatherhood.make(:user => @fred, :child => @ethel)

Note how there is an almost one-to-one mapping between the feature above, and the code below. The only thing cucumber does in this case is act as some kind of phoney translator. We write code, but not actual code. So we can do some stuff, but mostly it comes out worse than if we'd just written it as code in the first place. I can't blame anyone for disliking cucumber when using it like this.

However, try this instead:

Given there is a user called "Jimmy"
And there is a post authored by "Jimmy"

Given there is a person called "Fred"
And there is a person called "Ethel"
And "Fred" is the father of "Ethel"

There's not a huge difference between the first couple of lines, even though they read somewhat nicer when written out like this. The real difference is in the last line. Here cucumber is adding value by explaining this abstract concept of a Fatherhood into something very concrete: one person is the other's dad. Cucumber added value to this feature, instead of only acting as a hindrance.

I believe that Pickle is flawed as a concept, in order to achieve readable steps, they need to be written by hand.

The worst feature ever written.

As a curiosity, I present the worst cucumber feature known to man. If you are responsible for something like this, please go slap yourself in the face as hard as you can.

Scenario: User creates some sites and circuits, check connected sites list
    Given a "site" exists with {"name"=>"Somewhere1", "identifier" => "TER1", "provider"=>"TER1 Provider"}
    And a "site" exists with {"name"=>"Somewhere2", "identifier" => "TER2", "provider"=>"Some Provider"}
    And a "site" exists with {"name"=>"Somewhere3", "identifier" => "TER3", "provider"=>"TER3 Provider"}
    And a "circuit" exists with {"provider_name"=>"Another provider", "redacted_circuit_id"=>"ABC1", "provider_circuit_id"=>"C1", "circuit_type"=>CircuitType.find_by_name("Peering"), "service_type"=>CircuitServiceType.find_by_name("Dark Fiber"), :capacity => CircuitCapacity.find_by_name("1 Gbps"), "physical_wire_type"=>PhysicalWireType.find_by_name("Multi Mode Fiber"), "status"=> CircuitStatus.find_by_name("Cancelled"), "a_end"=>Site.find_by_identifier("TER1"), "b_end"=>Site.find_by_identifier("TER2")}
    And a "circuit" exists with {"provider_name"=>"Switch and Data", "redacted_circuit_id"=>"ABC2", "provider_circuit_id"=>"C2", "circuit_type"=>CircuitType.find_by_name("Backbone"), "service_type"=>CircuitServiceType.find_by_name("Dark Fiber"), :capacity => CircuitCapacity.find_by_name("1 Gbps"), "physical_wire_type"=>PhysicalWireType.find_by_name("Multi Mode Fiber"), "status"=> CircuitStatus.find_by_name("Cancelled"), "a_end"=>Site.find_by_identifier("TER1"), "b_end"=>Site.find_by_identifier("TER3")}
    When I am on the "connected_sites" page for site "TER1"
    Then the "connected-sites-list" should look like
      |   Site ID    |  Site Name | Site Provider | Provider Circuit ID  | Provider Name    | Circuit Status |
      |     TER2     | Somewhere2 | Some Provider |         C1           | Another provider |  Cancelled     |
      |     TER3     | Somewhere3 | TER3 Provider |         C2           | Switch and Data  |  Cancelled     |
    When I am on the "connected_sites" page for site "TER2"
    Then the "connected-sites-list" should look like
      |   Site ID    |  Site Name | Site Provider | Provider Circuit ID  | Provider Name    | Circuit Status |
      |     TER1     | Somewhere1 | TER1 Provider |         C1           | Another provider |  Cancelled     |
    When I am on the "connected_sites" page for site "TER3"
    Then the "connected-sites-list" should look like
      |   Site ID    |  Site Name | Site Provider | Provider Circuit ID  | Provider Name    | Circuit Status |
      |     TER1     | Somewhere1 | TER1 Provider |         C2           | Switch and Data  |  Cancelled     |

Yes, those are Hashes inside a feature, which are then eval'd. Make sure to scroll to the right to experience the full horror of it all. I challenge anyone to find a worse cucumber feature than this. I assure you, that thing is real (from one of our rescue mission projects), and there is much more where it came from.

Writing better steps

So how do we write better steps? For me personally, I've found that sticking to the following rule seems to lead to nice, maintainable steps:

A step description should never contain regexen, CSS or XPath selectors, any kind of code or data structure. It should be easily understood just by reading the description.

Jun

Fourth Day of Craftsmanship Swap


Steve Kim picked me up today again with his car. Only Doug Bradbury was in when we came to the office. A couple of minutes later came Justin Martin and we started to work on the same project as yesterday. At 10 we had the usual stand up meeting with the customer over Skype. We went through what we did yesterday and talked about today's stories. I got to work on a couple of stories on my own — I'm very happy to work on a well tested app. I was only going to add two fields to a model. When I was running all the test, two tests failed in another part of the application. I would never have know about this new bug I introduced if the test hadn't been there to save me. Thank you!

Lunch

During lunch Angelique Martin helped me check out of the hotel while Justin Martin and Colin Jones were buying some tasty hamburgers — with extra everything. Loved it. What I noticed was that their French fries is made of real potatoes. In Sweden we have this artificial stuff. That's kind of funny since we in Sweden think we are so healthy compared to Americans.

town

Evening

After today's work Micah Martin invited me to stay at his place for the rest of the week. They live in a lovely neighborhood. Micah and Angelique served a very pleasant dinner outside. Perfect weather and food. It felt like a top-notch swedish summer night, but then some mosquitoes came and started to harass us a little bit so we went in to watch a movie. And yes, we also have a lot of mosquitoes in Sweden. I almost forgot to write about their cute son Luca who really doesn't like to take a shower — it was a hard struggle before he finally washed his hair. What finally did the trick was that he could stay up a little longer. Thanks for a pleasant evening.

house

Tomorrow

I heard about the Elabs guys telling Eric Smith that I don't like pizza. That's not true. I'm just too vain to eat pizza for lunch — thinking about my curves or (I hope) lack of... so tomorrow we are going to eat some real American pizza. Looking forward to that.

Jun

Third Day of Craftsmanship Swap


A heavy rain rattling on the windowpanes woke me up at 6 o'clock at day three of the craftsmanship swap. After a big breakfast, Steve Kim picked me up and drove me to the office.

storm

Day three - Pairing in Ruby and Java

The day started with some pairing with Doug Bradbury. We did some debugging and fixed a platform specific bug in Mac OS and did some TDD programming in Java using JUnit. It has been about five years since I last wrote any Java code. It was fun, even though I have some difficulties to admit it. Doug Branbury, Steve Kim and I went for lunch to an excellent Thai restaurant, where I ordered Pad Thai. It was so much food that I had to take a doggy bag - my first ever. After lunch I was back on the same project as yesterday with Justin Martin. It's kind of a regular Rails project apart from that they are using JRuby, having their own gem bundler thing and using Prototype and JQuery at the same time. So maybe it's not that regular when I think more about it. I'm also liking AgileZen, the project management tool that we use in this project, more and more.

Evening - The A-team takes over

At the end of the day Justin Martin invited me to join him and Eric Meyer to go to the movies and watch The A-Team. I'm actually the only one of us that is old enough to have followed the TV series during the eighties. It was one of my favorite series during that period. The A-Team movie was fun but if you ask if I can recommend it, the answer is no. After an action comedy you always feel the need to dissolve the experience with a couple of beers before hitting the sack. Steve Kim also came and joined us at the beer place.

At the hotel - Key problems

Steve Kim drove me back to the hotel around 11 pm. For some reason, in the evening when I get back to the hotel the key to the door always stops working. When I finally got into my room, it was time to write this blog post about todays experience.

Good night!

Jun

Second day at 8th Light's Office — The Craftsmanship Swap


Last night Micah Martin helped me check in to the Hampton Inn & Suites hotel next to 8th Light's office.

hotel

Thunder all through the night

The moment I opened the door to the hotel room a big thunderstorm moved in over Libertyville. The lightning and thunder came closer and closer. But for me, it felt very cosy to hear the storm outside while I was inside in my king sized bed trying to sleep after a wonderful day.

Day 2

Day two at 8th Light started with a delicious breakfast at the hotel followed by a 500 hundred meter car drive to the office. Today I had fun pairing with Justin Martin on a regular Rails project that is using JRuby instead of MRI. I'm probably going to work on that project for the rest of the week. In this project they use an interesting project management tool: AgileZen, a nice competitor to Pivotal Tracker.

Food and Smelly Clothes

After an iChat stand up with the customer and pairing, it was time for Mexican food. Justin and I drove and bought tasty Burritos. After lunch and more pairing with Justin - food again. Li-Hsuan Lung and Steve Kim took me to a nice Korean restaurant. I liked it a lot apart from having smelly clothes afterwards.

dinner

See you tomorrow

Thank you 8th Light for this day and see you again tomorrow.

Jun

First Impressions of My Craftsmanship Swap With 8th Light


This Saturday, I left a rainy Göteborg at 7 am and I arrived 2 pm to Chicago, where Paul Pagel from 8th Light came and picked me up. Paul showed me around in Chicago. We went to the Millenium park and saw the famous Cloud Gate sculpture. After that we had a couple of beers and went to a barbecue party on a big terrace with a beautiful view over the Chicago skyline. I went home pretty early since I had been up for almost 24 hours. And of course I forgot to tip the taxi driver on the way to the hotel. It's not easy being a Swede and not being used to tip all the time.

On Sunday I took the Architecture River Cruise; there was a fantastic guide telling stories about the buildings. I actually learned one thing on the boat trip: the difference between modern and post modern architecture. Modern architecture follows the rule: "less is more" while post modern let the surroundings effect the form of the building. Therefore a modern building can be placed in any city and still fit in, while a post modern building only has one right place - the current. I can highly recommend that boat trip - it was awesome.

After only two days in Chicago I'm already in love with the town.

8th Light

This Monday morning I took the train from Chicago Union Station to Libertyville. I felt very excited to join all the craftsmen at 8th light. After saying hi to everyone I got to pairprogram with Doug Bradbury. It was a multi language project (C++, Java, Ruby). But fortunately we only programed in Ruby. The application handles reservations of computers in libraries. I learned a lot of new stuff today. The most profound thing I learned or the most extreme thing I did was to pairprogram with Doug when he was on a treadmill - that's extreme programming. I also got to work in IntelliJ for the first time. It's very nice but I'm still a Textmate guy, but since Ruby 1.9 still isn't working properly with all the bundles, I'm thinking about switching editor. IntelliJ might be the one.

NoSQL at Obtiva

In the evening, we went on to the a to Obtiva office's, downtown Chicago, for a NoSQL group meeting. They had a very nice office and served some delicious food. It was an open discussion around a paper (CAP - Consistency, Availability, and Partitioning) that everyone had read beforehand. Interesting but very technical. We had to leave early since we had to catch the train back to Libertyville, a one hour train ride, so we missed the last part of the group meeting.

Thougths

One hard thing coming from Sweden to USA, is to program on an american keyboard. Since we have the letters å, ä, ö, in our alphabet, the important "programmer keys" are positioned differently. So there's some fumbling on the keyboard while trying to find the right keys.

Building

Closing

So far it has been a great experience. I love the town and the people. I'm very glad for having this opportunity to travel from Sweden to Chicago and getting to know the 8th Light guys. Finally a big thank to Micah Martin and CJ Kihlbom for making this a reality. Now it's time get some sleep before a new exciting day at 8th Light.