Archive for

June 2006

Domain Driven Design

Right away you can incorporate the language of the code into your discussions with experts. In natural language, it’s equivalent to going from saying “well, to store this we’ll add an entry that tells us that a category belongs to a user and the user can have many news categories to tell us what they want to be told about” to saying “when someone wants to get News from our various Categories, we
create them a Subscription”.

Ok, maybe I’m exaggerating or overstating it a bit, but that’s the heart of the matter – language and communication. At the root of it is a focus on writing code for humans rather than machines. Coding in a way that can maximise the benefit you get from interacting with the domain experts.

Unfortunately, I hadn’t come across Evans’ excellent book Domain Driven Design at my last place, otherwise I think I would’ve made some different decisions and definitely framed my work better. However, on my current Java project with ThoughtWorks we have made efforts to focus peoples minds on the domain and to try and leverage the domain experts’ understanding as best as possible, and synchronise our code with it.

Last week I was pairing with a client developer and we performed a little model refactoring, naming concepts and relationships consistently with the notes and spontaneous diagram drawing we’d done during some quick domain model/discussion meetings.

The refactoring went pretty well (despite it throwing up some interesting issues with the introduction of a `Money` value object and resulting IBM `BigDecimal` and trying to store the aggregate within our Hibernate mapping).

We tried originally to use our own `CompositeUserType` but this turned out to be more work than it was worth and we took a hit on the DRYness and used Hibernate’s component mapping which actually turned out pretty good. The result is that we’ll have to define entries for our `Money` in each class that contains a `Money` value, but the value is it also stores the value inside the containing class which is good.

Ultimately, I’m more mindful now of how meaning is conveyed through the code and how it’s possible to incorporate natural domain language into the naming of concepts, and the introduction of new objects and classes where there’s value in the communicativeness. Of course, it seems rather common sensical but as always it’s easy to see how you just pass the obvious by (or at least I certainly have been vulnerable to it in the past).

It’s important that this kind of process is continual and evolutionary. Refactor as you go based on the knowledge you have. As the domain model changes, or your understanding deepens – change the code to follow suit. It’s amazing how much easier the resulting code is to talk about and build on.

Anything that makes it easy to write code in a manner such as this gets my vote. I’m looking forward to really learning Ruby and finding out how I can wrap it in such a way to write better code. After all, Rails provides some pretty nice language-looking bits for handling database mapping, web application handling etc. DSLs anyone? :)

Posted

Starting at ThoughtWorks and Domain Driven Design

So, where to begin really. Well, in a word it’s been awesome. I’ve never enjoyed joining a Company as much as ThoughtWorks, and have never enjoyed a project as much as the one I’m working on now.

One of the things I’ve really enjoyed has been the application of Domain Driven Design, and refinement of the ubiquitous language - the language of the domain (and consequently the language that’s used in both model and code). I can’t recommend Eric Evans’ Domain Driven Design book highly enough (despite it’s wordiness) for both high-level conceptual thought-provoking-ness and tips for implementation. Jeff Santini (our Iteration Manager) also remarked that it includes an example of one of the best examples of refactoring in any book (with regards to it’s shipping example) - showing how the code more clearly communicates domain understanding.

I’m over trivialising the first part of the book, but a lot of it’s focus is on the use of language, and aiming to keep the language the same between the domain experts, and developers. Incorporating domain objects in the discussions between developers and domain experts. The aim being that over time a ubiquitous language emerges, and the language of the domain is continuously refactored into the code. Ultimately, developers working on code (legacy or otherwise) can learn about the domain as they go. And through discussions with customers, can apply their changes and wishes more easily.

As the website says:


“the most significant complexity of many applications is not technical. It is in the domain itself, the activity or business of the user. When this domain complexity is not dealt with in the design, it won’t matter that the infrastructural technology is well-conceived. A successful design must systematically deal with this central aspect of the software.”

To this end, the book provides some suggestions on how to organise your domain objects, and encourages the use of the ubiquitous language in the naming of classes, and packages, so that everything becomes a valuable expression of the domain.

For instance, I can think of a previous project in my past role where we never captured the domain language, introduing our own names and inventing objects and then forcing them on customers. The result was that we couldn’t converse without attempting to translate, and that introduced inaccuracies and misplaced assumptions. The result was a messy, confusing and difficult to work with codebase. If we’d have dug a little deeper, and collaborated a little more, perhaps we could have ended up with a better model and more communicative code.

I’m yet to break 2/3rds of the way through the book, so there’s tons of useful stuff still waiting (so I’m told), but even so far I’d say it’s been invaluable in helping me approach this kind of business application.

There’s also some great advice about structuring your code, which I’m going to try and use to help me re-organise some of my pet Rails code. I’ll try and keep some notes and post here with updates on how it goes, see if it looks better and whether others can suggest further refactorings. But I digress!

Overall, the move has been one I’ve been very happy with. There’s no doubt I was nervous about starting, but people couldn’t have made me feel more welcome, and working with the other guys and girls on the team has been thoroughly enjoyable!

Posted
Fork me on GitHub