To DRY or Not to DRY: A Matter of Boundaries
DRY, or Don't Repeat Yourself, is a solid methodology to keep in mind along with SOLID principles.
Join the DZone community and get the full member experience.Join For Free
for a very very long time i've been convinced that aggressively refactoring your code in such a way that every piece of logic is repeated only once is a good thing. this practice, often referred to as dry, or don't repeat yourself, has been one of the many of my tools of choice and goes hand in hand with principles like s.o.l.i.d. some developers just are incredibly 'skilled' in cutting and pasting code all over the place, so i've been in need of this a lot over the years. but with any 'best practice', you tend to go through a couple of phases before you realize that the world is not black and white. just look at the adoption process of things like unit testing and mocking. so before you start to think i disapprove of dry, don't worry, i won't. but it's important to realize that even dry needs boundaries. it took me a few years…
so my first question is how far your efforts to apply dry go. if your codebase comprises of several projects all available from the same visual studio solution (or your favorite ide's equivalent), then chances are that that's how far you'll go. i doubt you will hunt down instances of the same code in other libraries and replace those with a single shared implementation. it probably won't even be possible. but what if that library's code was directly visible? what would you do then? the point is that the scope of your refactorings is kind of defined by the tool or source control repository you're working in.
my second question is about how you actually accomplish dry. do you introduce some shared project, component or folder that all your other code uses? and what appearance does that thing have? is it more like a library that you use? or does it feel like a framework that requires your code to implement some shared interface or inherit some common base-class? the difference is subtle but important. i'm not sure who said this, but a great phrase to illustrate the distinction is this:
"you use a library. a framework uses you."
i'm sure you have heard about design principles like decrease coupling, increase cohesion . the second part kind of that principle aligns well with dry, but the first doesn't. why? well, if you decide to move everything related to a particular concept to one place (and thereby increasing cohesion) and you use that thing everywhere, haven't you really created an immense amount of coupling? and that's exactly the flipside of the coin. but that problem isn't so bad if the thing you're sharing is just some utility code that you use. you can easily duplicate the code when it's time to extract parts of your system into something new. but if that thing you're sharing relies on some well-known interface that many parts implement in order to connect them together, the problem gets worse. especially if you're code base is large, this coupling gets sprinkled all over the place.
now that i've explained the dark side of dry and i've mentioned the scope of it, it should become evident that dry still has a place in every developer's toolbox. as long as you constrain its application to a well-defined boundary, you'll benefit from it. but outside that boundary? duplicate the code or wrap it in a really focused library which lifecycle is defined by another source control repository. if you're in the .net space, you can solve this even nicer by using source-only nuget packages and distribute them through myget . i realize that it may feel unnatural to duplicate code within the same source code repository. but imagine that those boundaries were used to split up the code in different solutions or repositories. i doubt you would try to apply dry there. next to that, in my experience, trying to use the same code in different places tends to put you on the path of either overly generic or leaky abstractions. better to have two, more specific implementations instead. frameworks are notorious for this, so if you really need a framework-ish solution, be very very careful to decouple the code that needs it from the framework (and .net delegates solve this pretty nicely).
identifying the right boundaries is the difficult part, although i'm fairly confident in saying that your ide's solution structure is never the right boundary. if you're practicing domain driven design and you have identified the bounded contexts , you have a great starting point. you still might want to find a smaller boundary within that such as for instance groups of classes that always change together, but never go beyond the bounded context. architectural layers should also be seen as boundaries, even if the code is grouped in the same ide project. a more functional view can be very beneficial as well, especially if you can identify vertical slices of functionality. greg young recently shared a more inside-out approach by stating that you should design your code in such a way that you can rewrite any part of it in a single day. it’s a rather extreme approach, but it will help against overzealous use of dry.
so what do you think? do you still believe in dry or did you abandon it completely? and if still practice it, how do you define the boundaries and seams in your system? i'd love to hear your thoughts by commenting below. oh, and follow me at @ddoomen to get regular updates on my everlasting quest for better solutions.
Published at DZone with permission of Dennis Doomen, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.