Object Calisthenics: First thoughts
We ran an Object Calisthenics variation of Coding Dojo on Wednesday night as part of ThoughtWorks Geek Night in Sydney.
Object Calisthenics is an idea suggest by Jeff Bay in The ThoughtWorks Anthology , and lists 9 rules to writing better Object Oriented code. For those who haven't seen the book, the 9 rules are:
- Use only one level of indentation per method
- Don't use the else keyword
- Wrap all primitives and strings
- Use only one dot per line
- Don't abbreviate
- Keep all entities small
- Don't use any classes with more than two instance variables
- Use first-class collections
- Don't use any getters/setters/properties
We decided to try and solve the Bowling Game Problem while applying these rules. We coded in Java as this was a language everyone in the room was comfortable with. It would have been cool to try out Ruby or another language but I'm not sure if this type of setting is the best place to learn a new language from scratch.
I hadn't arranged a projector so we couldn't adopt the Randori approach. Instead we split into three pairs rotating every half an hour, discussing how each pair was approaching the problem at each change.
Learning from the problem
I was surprised how difficult the problem was to solve using the Object Calisthenics rules. There were several occasions when it would have been really ease to expose some state by introducing a getter but we had to try another way to attack the problem.
We have been following the approach of wrapping all primitives and strings on my current project as 'micro types' so this rule wasn't new to me but the general feeling early on was that it was quite annoying to have to do. From my experience on my project it does help to encourage a more object oriented approach of keeping the data with the behaviour.
This approach to object orientation is very extreme but the author suggests giving it a try on some small projects as being able to code like this will result in you seeing problems in a different way. I noticed today that I was always on the lookout for ways to ensure that we didn't expose any state so it's had a slight influence on my approach already.
We had an interesting discussion about mid way through about whether we should implement equals and hashcode methods on objects just so that we can test their equality. My general feeling is that this is fine although it has been pointed out to me that doing this is actually adding production code just for a test and should be avoided unless we need to put the object into a HashMap or HashSet when the equals/hashcode methods are actually needed. The only alternative I can think of is to not test object equality and instead only test equality where we have primitives or to test for equality by using reflection.
From seeing the approaches others had taken I realised that the approach we took on my machine was too difficult – we would have been more successful by adopting baby steps.
Learning about the format
We initially started out trying to design a solution to the problem on a white board before getting to the coding but this didn't work particularly well so we abandoned this and went straight to the code.
Each machine had three different pairs working on the problem over the duration of the night, with one person always staying on the machine and the others rotating. While we all had slightly different approaches to the problem it would have been interesting to see if we could have progressed further using the Randori approach with everyone having input to the same code base.
None of the pairs managed to complete the problem, and there was concern that the problem was too big to fit into the 90 minutes we spent coding. After speaking with Danilo and reading his Coding Dojo paper it seems that this is not necessarily a bad thing and the focus is supposed to be more on the learning than problem completion.
It was certainly an interesting experience and I had the opportunity to work with some people that I haven't worked with before. We are hopefully going to make these Coding Dojos a regular feature and try out some different approaches to see which works best for us.
On this occasion I selected the problem but in the future we would look to make it a group based decision depending on what people are keen to learn.
This is a small point, and you probably are already aware, but the write up did not mention it.
implementing equals and hashcode on a mutable object can create problems.
Any object which you want to compare for equality, might need to be immutable. Otherwise what does the equality mean.
Eric Evans talks about mutable versus immutable in terms of entity versus value objects. as do plenty of other sources.
Jeff Santini
7 Nov 08 at 8:31 pm
It sounds like the experiment would have been better if the pairs attempted to solve the problem using whatever techniques felt most natural to quickly solve it, and then attempt to refactor to the rules set forth in Object Calisthenics.
I think that this parallels the idea of getting to green as fast as possible when doing TDD. An extension of that is to get to business value as quickly as possible – get the feature or features done, and then reward yourself with some refactoring.
While I don't advocate that absolutely (we shouldn't throw out our principles while coding), I've seen developers that spend large amounts of time tweaking and beautifying their code without having completed anything of real value. I've also made this mistake in the past. A beautiful piece of software that doesn't do anything is worth less than a complete piece of software that suffers from some poor implementation details.
Kris Kemper
8 Nov 08 at 5:04 am
Great post!
I've tried the same Object Calisthenics variaton in a dojo before and it was really interesting. In our case the problem was minesweeper and we also didn't finish the solution. On the other hand was really good to try an approach which would be really hard to introduce in production code.
Our conclusion at the time was that the idea is better as a guideline instead of rule. I'd like to try it again now to see if people still agree with that.
Good to read your experience in the same approach. Contratulations for the dojo!
Ivan Sanchez
10 Nov 08 at 7:20 am
[...] Coding Dojo on Wednesday night as part of ThoughtWorks Geek Night where we continued working on the Bowling Game problem from last week, keeping the Object Calisthenics approach broadly in mind but not sticking to it as [...]
Coding Dojo #2: Bowling Game & Object Calisthenics Continued at Mark Needham
13 Nov 08 at 10:41 pm
[...] the year where I realised how much more could be done by sticking to the principles. We tried out object calisthenics in some coding dojos and I learnt a bit about the idea of managing the flow of an application [...]
2008: My Technical Review at Mark Needham
1 Jan 09 at 9:30 am
[...] reading Object Calisthenics and working with Nick I have got used to wrapping collections and defining methods on the wrapped [...]
C#: Wrapping collections vs Extension methods at Mark Needham
23 Feb 09 at 8:27 pm
[...] week's dojo involved coding a familiar problem – the bowling game – in a different language, [...]
Coding Dojo #18: Groovy Bowling Game at Mark Needham
26 Jun 09 at 6:16 pm
[...] we weren't intentionally following the ideas laid out in object calisthenics we actually ended up with code which fairly closely followed those ideas. We had very small [...]
Coding Dojo #20: Groovy Sales Tax Problem at Mark Needham
31 Jul 09 at 9:08 am
[...] might have heard about Object Calisthenics before, on this blog or other resources on the net. Perhaps you've read the original article, which is highly advised. In short, [...]
Follow-up to our Dev Brunch November 2009 « Schneide Blog
30 Nov 09 at 1:03 am