Archive for May, 2013

This week I published my first book! I’ve been writing “The Coding Dojo Handbook” since last September, and publishing it as a work-in-progress on Leanpub.com. This week I decided it was time to declare it completed, since I think it hangs together as a whole book, and is useful in the role I imagined for it. In other words, I think this book has everything it needs to be a good starting point for someone setting up a new coding dojo, or for someone experienced in running one already, looking for ideas for new katas and collaborative games. I hope you’ll consider getting a copy if you’re in either of those situations!

Now the book is finished, I have to decide whether to look for a “real” publisher, or whether to just continue to sell it on leanpub. My current feeling is that my target audience, (programmers), are quite comfortable buying an ebook, and having a paper copy isn’t really a priority. The advantage of a publisher might be more sales channels, bookshops etc, and more copies sold overall. I’d also get a considerably lower proportion of the sale price. I’ve noticed that several authors I respect – people like Brian Marick and Roy Osherove – are publishing their newer titles exclusively on leanpub.com. So my current plan is to stick with leanpub and see how things develop.

I had originally planned a few more chapters, about London School TDD, and Approval Testing. When I started writing these chapters, I found I had far more to say than I had anticipated, and it didn’t seem to me that the material really fitted into this book. So what I’ve done is started a new book project, called “Mocks, Fakes and Stubs”

Right now it’s fairly small, more a pamphlet than a book, and I’m not charging a lot of money for it. If, as I hope, there is interest, I plan to add more material over the next few months. The focus is on showing TDD techniques using some of the code katas from “The Coding Dojo Handbook“. I’m hoping the new book will have the feeling of pair programming with an experienced coder, explaining the theory of a technique at the same time as demonstrating it.

I’ve got a couple of workshops coming up, at XP2013, when I’ll be doing research for my new book. Basically I’ll be using code katas to explore TDD techniques like Outside-In, Approval Testing, and Given-When-Then style BDD tests.

So my first book is finished, and I have a new book project to occupy my time!

 

I’m pleased to announce a new home for my blog: henceforth I will be blogging on the url “coding-is-like-cooking.info“. Please update your RSS feeds to point at the new site!

At some point I plan to delete my blogger account, since all the articles have been transferred to the new url. (If you find links between articles that don’t work on the new site, please let me know, there are some aspects the automatic import doesn’t handle very well.)

I’ve been using Blogger for years, and I can recommend it as being easy to use and quick to get started with. What’s prompted the change is that I really wanted to take full control of my content and the way it’s presented. Since I started this blog 6 years ago, it has turned from a hobby project into an important channel for me to present my newest ideas and get feedback from my peers in the community.

Over the last 6 months or so, the traffic on my blog has exploded. I used to average about 1000 page views per month, but since mid 2012 I’ve been consistently getting 3000-4000 page views per month. That huge peak in September 2012 corresponds to my article “SOLID principles and TDD”, which received lots of comments and tweets. The most recent peak corresponds to my follow-up series of posts about London School TDD.

Monthly Page views for "Coding is Like Cooking"

Monthly Page views for “Coding is Like Cooking”

I think the reason for the increased popularity of my articles is that I’ve been spending more time writing, generally. I’ve been doing more research, more background reading, and working on my writing style. Some of my recent blog posts have also ended up becoming chapters in my book.

I hope all my readers will continue to follow my writings in their new home, on coding-is-like-cooking.info. I have more great articles planned!

This is the third post in a series about London School TDD. The first one is here, introducing the topic. The second post discusses “Outside-In Development with Double-Loop TDD”. In this post I’d like to talk about the second difference I see between Classic and London School TDD, which is to do with your style of Object Oriented Design.

“Different design styles have different techniques that are most applicable for test-driving code written in those styles, and there are different tools that help you with those techniques…

That’s what we  … designed JMock to do …
“Tell, Don’t Ask” object-oriented design.”

— Nat Pryce, in an email to a discussion forum.

That quote explains the objective Nat et al had when designing JMock, and I think it shows  that London School TDD is actually a school of design as much as a testing technique. Let’s take a closer look at this way of designing objects.

Tell, Don’t Ask

“Tell, Don’t Ask” Object Oriented Design is about having Cohesive objects that hide their internal workings. If your objects obey the Law of Demeter, that’s a good start, it means they hide their inner workings and don’t talk to objects far away on the object graph. It reduces Coupling in your system, which should make for better maintainability.

In their book “Growing Object Oriented Software, Guided by Tests”, Freeman & Pryce actually define “Tell, Don’t Ask” as the same as following the Law of Demeter (p17). Then they go on with several chapters about their design style, expanding far beyond simply “following the Law of Demeter”. It’s well worth a read, here’s a sample:

“… we focus our design effort on how the objects collaborate … obviously, we want to achieve a well-designed class structure, but we think the communication patterns between objects are more important.”

— Freeman & Pryce, GOOS, p58

Message passing vs Types with Data

So it’s basically about how you view your objects. Do you see them primarily in terms of sending and receiving messages to other objects in order to get stuff done? Or are you more focussed on the data your objects look after and the class of objects they are part of?

In the diagram below you can see an object is defined in terms of which messages it sends and receives:      london_school_008

This diagram shows the same object, but with a focus on data rather than messages:

london_school_007

If you have a “message” focus, you’ll be concerned with defining protocols and interfaces. You’ll worry about which collaborators will be needed to process a particular message. If you have a “data” focus, you’ll be interested in checking your object goes through particular state transitions. You’ll check it makes correct calculations based on its data, and hides whether the result is cached or calculated.

In my first post I talked about the three ways to verify object behaviour. In Classic TDD, the most popular way to write your assert is to check the state of the object you’re testing, or a collaborator, using a public API. This naturally leads you to design objects that are more type-oriented, with the emphasis on class relationships.

In London School TDD, the favoured way to write your assert is to use a mock and check a particular interaction happened, or in other words, a particular message was passed. This is because you favour a design where objects don’t reveal much at all about their data – your system is all about the interactions.

Dependencies and Collaborators

In a message-oriented design, it’s natural to want to specify which collaborators a particular object needs in order to get something done, and what messages it will send them. It’s part of the public specification of an object, and natural to pin down in a test case using mocks. If you instead check your object via a method that lets you query its state, it could expose details that might stop you refactoring the internals later. This leads you to prefer to check your messages, rather than state and data.

If you have a more type-oriented design, you may want to hide the fact you’re storing data accross several objects, or delegating certain calculations to other objects. Those dependencies aren’t part of the public specification, what matters is the end result. If you start exposing these interactions in your test via mocks, you’ll end up with brittle tests that hinder a subsequent redistribution of responsibilities between an object and its dependents. This leads you to prefer to check state and data, rather than interactions.

Comparing the two styles

In these articles I’ve tried to draw each style of TDD to an extreme in order to emphasize the differences. Of course, in practice, a competent developer will use the style most appropriate to the situation she finds herself in. She may use both styles while developing different pieces of the same system. In my next post, I’d like to illustrate this with a small example.