Musings

Object Calisthenics Kata

In order to keep your coding skills up to scratch, it’s important to practice, practice, practice. Us techies have a fancy word for “practice exercise” – Kata – but essentially all a Kata is is a problem which is not too complex, for which one can apply various rules or just practice concepts like TDD.

This week, I attempted the Bank Account kata using the rules of Object Calisthenics (a defined set of extremely strict rules which, while very difficult to put into practice, lead the developer to think in an object oriented fashion)

I really enjoyed the experience, and thought I’d document some of my thought processes here.

I started, in true TDD fashion, with the test. Since we had some concepts defined, namely Account and Amount, I proceeded to jump straight to using these. Writing the test in itself was half the exercise!

Below are some of the issues I faced:

– I wanted to create an instance of Account for the payer, with some initial balance. Usually, I would have just passed an int into the constructor for Account, but the rules state I must wrap all primitives in their own class – so I created another class called Balance which was initially just for storing the initial balance of the account. This didn’t make any sense – the balance can change – so we’d want to add or subtract an amount – so maybe the Balance class should contain an Amount, rather than a simple int! What a kerfuffle.

– Once that had been sorted, I had to then write the assertion. I’m used to writing a simple assertThat(class.getValue).isEqualTo(expectedValue), but of course the rules state I cannot have any getters/setters! This left me slightly flummoxed – but then I realised the class itself could check its own equality, and all we would need to do from the test is check that the equality holds true for our expected amount.

I finally had a red test! First stage of the TDD cycle – check :). Now to make it green…

This took a lot of thinking about to ensure that the responsibility for the work was given to the correct class. My rule of thumb for working towards this goal was basically, write what I want the code to do! I ended up with the following method in my Account class:

public void transferTo(Amount amount, Account receiver) {
        this.reduceBalanceBy(amount);
        receiver.acceptTransferOf(amount);
    }

Using the fantastic alt+Enter functionality of my IDE, the methods slowly started forming themselves in the correct places. For example:
– the above led to a reduceBalanceBy method created in Account
– this method updated the balance field in Account and added an Amount to it
– this meant the Balance class now had an add method, so it needed to find some way of adding an Amount to a Balance.
and so on…

What I found interesting here was, because we are forced to wrap all primitives, we end up with a method being delegated down until it reaches the level where the information is accessible. This forces us to think hard about which class should actually be adding the integers together, for example – i.e. which OBJECT is responsible for the information we are looking at.

Whilst on a day to day basis it is extremely easy to simply get information from the classes we are dealing with, or to set values on other classes, these rules make us reevaluate how we use our objects. Rather than an external class doing the work, a much more object-oriented approach would be for visibility of properties to be restricted to a separate object which handles them.

It’s important to note that whilst these rules make one think in a more OO way, they are nigh on impossible to implement all the time, and indeed should NOT always be implemented, or we would end up with very ugly code. However, it is useful to do your own exercises with the rules in mind, so that your way of thinking changes, and you can then use this new perspective in your day-to-day development. I strongly recommend the exercise to everyone – it is amazing how we are so used to doing things a certain way! It’s always good to challenge that sometimes :).

Just writing this first, seemingly simple, scenario took me 45 minutes to get right (and I had absolutely no idea where the time had gone!). It’s a great challenge to the mind, so go ahead – try it!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s