Friday, January 30, 2015

Kata: Happy Numbers in Clojure

I've just dome the Happy Numbers kata in Clojure.

It's a simple kata that can be a lot of fun.

I used a mix of TDD and REPL-driven development to code it.

These are the resulting tests in Midje:

and this is the code:

To document the process I commited the code after every passing test and every refactoring. I also commited the REPL history.

You can find the commits step by step here and the code in this repository in GitHub.

I used memoize to avoid repeated computations.

In the next days I will try other suggested optimizations based on the properties of happy numbers.

Interesting Talk: "Concurrency"

I've just watched this two lectures about concurrency from the CS 61A course from Berkeley University:

Monday, January 26, 2015

Refactoring Conway's Game of Life in Java

I've recently revisited the Java version of Conway's Game of Life that I did nearly a year ago, to refactor it using some of the ideas I used in my more recent Clojure version.

This is the UML for the refactored design:


I eliminated the LivingCells class that appeared in the previous design.

I pushed a lot of code from the GameOfLife class into Generation to eliminate a bit of feature envy. This is GameOfLife's code now:

and this is Generation's one:

I also refactored the tests eliminating all the checks based on strings and using constructors with variadic parameters.

Following the idea in the Clojure version I managed to simplify the Rules interface a lot:

Most of the other changes are just renaming of methods, variables and parameters.

This new code keeps the functionality and characteristics of the previous version but it's much simpler.

Sunday, January 25, 2015

Kata: Bank Account in Java

Álvaro and I have been pairing lately to do a subset of the Bank Account kata that Sandro Mancuso proposed as an exercise in his Crafting Code course.

We had to write and make the following acceptance test pass using Outside-In TDD:
Given a client makes a deposit of 1000.00 on 01/04/2014
And a withdrawal of 100.00 on 02/04/2014
And a deposit of 500.00 on 10/04/2014
When she prints her bank statement
Then she would see

DATE | AMOUNT | BALANCE
10/04/2014 | 500.00 | 1400.00
02/04/2014 | -100.00 | 900.00
01/04/2014 | 1000.00 | 1000.00
The goal of the exercise was to understand the outside-in or London-school TDD style.

After the course Álvaro and I continued working on the exercise.

We had many difficulties to get used to this style for several sessions because we weren't used to test behavior instead of state. We struggled a lot because we were working outside of our comfort zone. In one of the sessions, we rushed too much trying to move forward and make the acceptance test pass, which resulted in allocating some of the responsibilities very badly.

In the end, we managed to get out of the mess we got in by slowly refactoring it to a better design.

All in all, it's been a great experience and we've learned a lot pushing our limits a bit further.

Unfortunately, we didn't commit the code after each tiny step this time, only at the end of each pairing session.
You can find the resulting code in this GitHub repository.

As usual, it's been great pairing with Álvaro, thanks mate!

Thursday, January 22, 2015

Refactored Numbers Spelling kata in Clojure

I've refactored the Numbers Spelling kata I recently did in Clojure to remove some duplication.

Before refactoring it, I had to make the old spell-out-number-over-99 and spell-out-number-up-to-99 functions similar enough for the duplication pattern to emerge.

Then I could use the same function for both cases and also made some other minor changes such as using when-not form and renaming several bindings and arguments.

This is the refactored code:


You can find the code in this repository in GitHub.

Tuesday, January 20, 2015

Kata: Numbers Spelling in Clojure

I've just done the Numbers Spelling kata in Clojure:

These are the tests using Midje:

and this is the code:

To document the TDD process I commited the code after every passing test and every refactor. You can find the commits step by step here.

If you look at the commits, you'll notice that it took me some time to find a simple algorithm using TDD but once I found it I could simplify the code quite a bit.

You can find the code in this repository in GitHub.

Saturday, January 17, 2015

Interesting Paper: "Design Patterns in Dynamic Programming"

I've just read the slides of a very interesting talk by Peter Norvig:

Reading GOOS (XI)

These are the links mentioned in our last conversation about the 15th chapter:

Clojure Developers Barcelona: Reverse Polish Notation Calculator kata

Last Tuesday I facilitated a kata for the Clojure Developers Barcelona Group hosted by Akamon.

We've been doing several Clojure katas and talks during the last two months within a small study group (mainly composed of people who work in Akamon) as a preparation to bring back to life the Clojure Developers Barcelona Group.

This kata was the first one we did as Clojure Developers Barcelona Group.

We did the Reverse Polish Notation Calculator kata which is a very nice kata to practice.

Although 10 people had RSVPed that they were coming, in the end we were seven people: 4 Akamon workers, 2 people non-related to Akamon (a second-timer and a newcomer) and me. We formed 3 pairs and worked in the kata from 18:30 to 21:00.

I had a great time facilitating the kata, solving doubts and talking about Clojure.

If you like you can share your solution in the Clojure-Barcelona Google+ community. At the moment of writing this post, you'd find there two solutions: Rafa's and mine.

To finish I'd like to thank Akamon for hosting our events and all the attendants (Jorge, Jordi, Sergi, Álvaro, Rafa and Eloi) for their enthusiasm to learn and share in an afternoon of Clojure.

Saturday, January 10, 2015