Monday, October 17, 2011

Conditions and Dictionaries

Last week I was involved in some new employee code reviews and pairing interviews. Since most people that apply to ThoughtWorks do our Mars Rover problem, I decided that I should probably give the problem a try. The solution is pretty simple, which is probably why most people select this problem. I happened to be in a programming mood at the time, so I decided to make the problem a little more difficult.

Could I write a solution without an IF statement?

It turns out that I can and the results were very interesting. Here is the first cut of my spin left method before the change over.



Here is another deviation with if statements, because you know... switches are smelly.



Then here is what I came up with when trying to remove all my conditions.



Personally, I find it much more readable than the other two. This is something I would not have expected given an arbitrary challenge. Maybe those anti-if people are onto something.

Saturday, October 01, 2011

Tackling Business Complexity

This week at a client site, one of our stories came back from the dead because some condition wasn't handled. As we were implementing the story, our understanding increased and along with understanding came new questions. After asking these new questions, it became clear that there was a lack of holistic understanding for the both the developers and the business. The developers wanted a rationalized set of conditions, the business wanted things to be displayed in different ways when certain conditions were met. Basically, we wanted the same thing, but could not meet on a common language.

The thing we could agree on was what parts were important, so I came up with the idea of producing a truth table to hash out the different combinations. It looked a lot like the image below.




After creating the table, it was a matter of going through each combination and recording what expected result should be displayed. This produced five potential outcomes that turned out to be relatively simple to handle.



Looks almost too easy right? I'll assure you that the vernacular of the story did not cover the problem in such logical terms. The distillation of these guard statements (each condition returned) was a direct result of the truth table. Had we gone with a more classical approach, it would have likely resulted in a fair bit of cyclomatic complexity or the introduction of more classes to tackle this problem through polymorphism.

As it turned out, we wrote some tests, wired everything up and it worked just like the BA wanted, but could not logically define.