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

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.