Refactoring towards deeper insight

While giving my DDD presentation at the Jacksonville Code Camp, I felt that DDD refactoring was one area that I really didn’t touch on enough. I mentioned it briefly but I wanted to build on the discussion that we started to have in the presentation. I feel this is a very important practice of DDD and needs more explanation.


When doing DDD, a very important concept is the fact that noone gets the model correct the first time. This is against the traditional waterfall approach of BDUF (big design up front) and trying to set everything in stone from the beginning. Because of this reason, the domain model needs to be refactored throughout the development lifecycle. In order to support this constant refactoring, you need to use TDD in order to keep your model functioning as expected. Without the safety net of tests, you have no way of seeing how the changes impact other portions of the system.


In my presentation I spoke about two areas of refactoring in relation to DDD, or refactoring in general. Timing and Intiation. There are other concepts surrounding refactoring that I won’t focus on here.


Timing of refactoring deals with when refactoring should occur during the development. Refactor too early and you will spin your wheels, wait too long and it becomes difficult to change the model and more costly. There is a sweet spot for timing to perform refactoring. I would say its a fairly large window as you will start to get code smells, this is an indicator that refactoring needs to occur, along with the ubiquitous language changing. The refactoring timing is fairly easy to see and show take place once you see it coming. Don’t hesitate or put it off until later, it will hurt much more if you do.


Initiation of refactoring is important as well. Once you notice that refactoring needs to take place the second step is actually initiating the refactoring. It’s extremely easy to start cowboy coding doing refactoring changes because you have the tests, but this shouldn’t be the case. It’s very tempting but try to avoid this. Instead, begin writing tests to drive the refactoring in the direction you wish to go. If you notice that an entity has been renamed in the language from Loan to LoanSomething, then start by renaming it in your tests, which will then drive you to rename it in the domain. This is how all refactorings should take place in general, not only in DDD. Choose portions to localize the refactorings, but at the same time to just take little nibbles off the model. Try to cover as much of the domain model as required without making huge refactoring changes. The idea here is to perform small incremented refactorings that increase the cohesiveness of the model while not make huge sweeping changes in the process.


While not the most important or well documented aspect of DDD, Refactoring plays an integral role when performing DDD. It feels slow and sluggish at first, but once you get the hang of it, you can utilize your toolset to aid in the refactoring, such as CodeRush or ReSharper. These greatly speed up the refactoring process in various ways. Don’t be afraid or nervous to start refactoring, you will introduce problems but quickly iron them out in the process. Do this often enough, with the correct methods and your domain model will become much more flexible and cohesive for future changes. The domain model is NEVER set in stone. No matter who you are, noone will get it correct the first time. I really want to stress that point. Be comfortable and accept that so you can refactor towards a better model. Do this on an often enough basis and you will drive the domain model to a deeper insight that better reflects what the core idea of the domain model is. Hence, refactoring towards deeper insight.


I hope that clears up anything I may have left out of my presentation and further defines the role of refactoring in DDD. Comments, Questions?

Related Articles:

Post Footer automatically generated by Add Post Footer Plugin for wordpress.

About Sean Chambers

I am a Senior software developer from Palm Coast, Florida. An advocate of Domain Driven Design, Behavior Driven Development, creator of FluentMigrator and community activist. I am married to my beautiful wife Erin and am the proud father of two wonderful children. I currently reside at ACI, a local insurance industry/mortgage software company that excels in creating solutions using Agile methodologies.
This entry was posted in Uncategorized. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • http://www.bluespire.com/blogs Rob

    Would love to see you do the DDD talk at Tallahassee Code Camp!
    http://capitalcitydotnet.net/dotnetnuke/CodeCampIV/tabid/64/Default.aspx

  • joe

    I see an awful lot of chatter about DDD and it seems like every other .net blogger practices it. But in every interview I read read or watch with Eric Evans, he stresses that it is not always the route to take. Since you asked for questions — when is DDD overkill and at which point does it produce benefits?

  • http://www.lostechies.com/blogs/sean_chambers schambers

    @joe,

    There is definately instances where it is not applicable. Usually, applications with small/no core domain aren’t applicable for DDD. There are other instances, but this is usually a good rule to follow.

    There has been several applications I have built that I steer away from DDD for this reason.

    Once you decide to use DDD for the basis of a domain model you incur a bit of overhead in using it, thus it should be initially discussed if there is a good domain language/model to utilize DDD with.

  • http://vemruvnrqgvz.com/ ybodlte

    jo4ZVn dzrxmlyndytj, [url=http://cajamfycuhql.com/]cajamfycuhql[/url], [link=http://mzmyurzkahuu.com/]mzmyurzkahuu[/link], http://tdhmjlosmepl.com/

  • http://yimehdfnmisv.com/ clpeeyrmo

    qUzcCK wrqseiyprrfn, [url=http://exxxfshwuukh.com/]exxxfshwuukh[/url], [link=http://onjrbjtnbzbi.com/]onjrbjtnbzbi[/link], http://fvtxccevkggo.com/