Tuesday, 30 June 2015

DRY vs Coupling in Production Code

Duplication in tests can be a good thing. The same can be said for production code as well in some cases. No. I'm not going mad. I probably wouldn't believe this if you showed me this several years ago either. More experience has shown me that loose coupling is often more desirable than removing duplication. In other words, the more duplication you remove, the more coupling you introduce inadvertently.

Duplication of logic is bad and will always be the case. I am not debating this. You should only have one logical place for any domain logic. As always follow the DRY principle. However just because two pieces of code look the same, does not mean there is duplication.


A system from my past had two places where an address was required for display and serialization. Billing and Delivery addresses.

My gut reaction was to introduce a common address model that could be used for serialization and display. After all this screams of duplication. However a billing address and delivery address are two conceptually different things despite appearing identical.

Given time the needs of the billing functionality may very well differ from the needs of the delivery domain. Duplication of models/contracts is weak duplication. There is no logic here.

In DDD each bounded context will have different needs. As it turned out the Billing Address began to have specific billing related functionality added such as "IsDefaultAddress" and "IsSameAsDelivery". At this point the two models are very different. This was a problem.

Sharing via a common library would have removed the total lines of code but increase the number of dependencies. The Address is now coupled to a single form meaning updates and new requirements are harder. Versioning and packaging are now a concern. Any updates would need to be coordinated across teams. Udi Dahan has warned about this previously in what is summarized as "Beware the Share".


This example makes inheritance look like a good fit. While the use of inheritance when applied correctly is not a bad thing, this scenario is not appropriate. Inheritance is one of the strongest forms of coupling. Applying inheritance across a type that we don't own is risky for the reasons detailed previously. Now change is not only harder, it would potentially be a breaking change. How would we model a delivery address with multiple addresses? Why should both the billing and delivery domain use the same terminology for its fields? If we accept that both addresses are conceptually different despite looking identical at present, we can side step these issues.

What to Share?
  • Domain types should be shared. Using the previous example a PostalCode would make a good type to share. The functionality here is identical regardless of the type of address. PostalCode would likely have logic associated with the type which would not make sense to duplicate or implement in each sub system.
  • Shared functionality that must be consistent makes a good candidate also. Examples such as UI widgets including headers and footers.
  • Crossing cutting concerns such as logging, security and configuration can be shared when appropriate. A downside to this is you now force your consumers to take specific dependency versions which may or may not be acceptable.
Shared Kernel

DDD has the concept of a Shared Kernel. The dictionary definition of a kernel is "the central or most important part of something". Shared Kernel's make sense to share the common functionality previously. The name "common" is poorly thought out however. Most codebases will have a common or utility library but by there very nature these will grow into large components.

The reason for this growth is everything is common across applications. All applications need some sort of data access, so stick it in the common library. All applications need some sort of serialization mechanisms, so stick it in the common library. All applications need some sort of web technology, so stick it in the common library. You should be able to see where this is going.


As always when dealing with duplication apply the Rule of Three where appropriate. If you really must create a shared component, a small, concise library is better than a library that handles multiple concerns. This will allow consumers to adopt a "plug 'n play" approach with which components they require. Even then, try to fight removing duplication unless you can be really sure there is a good reason to increase coupling. That reuse you are striving for might not even come to fruition.

Tuesday, 23 June 2015

FirstOrDefault in LINQ

Explicit null checking is a code smell in most cases. It should be limited where possible, or pushed to the edge of the system. A common anti pattern I've noticed is the incorrect use of First() in LINQ, which I have used myself on many occasions in this manner.

Assuming a collection of items that you wish to query, the incorrect approach is to explicitly check for a null return value and act accordingly.

The use of FirstOrDefault() is redundant because no default is actually set. The default value of a reference type would be null. Meaning the explicit null checked is required. We could use First() alone, but this will throw an exception if there are no elements to query against.

A better solution is to set the default. As long as our initial query is not operating on a null reference this is safe. Here the explicit null check is gone. We have replaced it with a more functional solution which is after all what LINQ is based upon. While both are equivalent, the second example is much cleaner as well as being open to further chained statements.

First() relies on one or more items being in the sequence. When you are only ever dealing with one result Single() is more appropriate. This method will throw an exception if more than one result is found, acting as a form of assertion. Like First(), Single() offers SingleOrDefault() which would work in the same manner as above.

Wednesday, 17 June 2015

Do you really need a Microservice?

Lately there has been two sets of advice around the use of Microservices. Some advise that Microservices should be built after the fact. Others advise the opposite solution. In conjunction there is a third option that deserves more attention. Do you even need a Microservice at all? A recent tweet sparked off the exact thought I have found myself conveying.

Creating a Microservice is no easy feat. Despite the limited code or functionality that is involved. There is a whole host of things that need consideration; source control, project setup, databases, project conventions, monitoring, logging, deployment, hosting and security to name a few.

The so called monolith or "application" as it was known before is a tried and tested way of structuring applications. One of the big criticisms levelled against monolithic applications is coupling. Having worked with some terribly coupled applications I agree fully with this complaint, but there are steps you can take to prevent this.

A whole application does not need to live inside one logical project, but instead can be split appropriately. In DDD a Bounded Context makes sense. Using this model you would end up with a Customer and Products project rather than a single project containing both.

An easy step is the correct use of namespaces that are structured by features rather than technology choices.

Education is also important, simply put in agreements across teams such as "nothing from the customer project will directly reference the products functionality". This can be taken a step further by introducing assertions into the build process that will fail if "project A references project B".

There is no correct answer on whether you should or should not start with a Microservice architecture. Each team will need to judge and base their answer on their needs which will most likely vary over time. As it has been said before - if you can't structure a monolith what makes you think you can structure Microservices any better?

Tuesday, 9 June 2015

Branch by Abstraction

Feature toggles are great for new features or features that are either enabled or disabled. Branch by Abstraction offers the same benefits as feature toggles but the seam to introduce the change is the abstraction itself. Unlike Feature Toggles, the use of Branch by Abstraction allows a gradual transition to new functionality.

Start by duplicating the type or implementing a new version of the abstraction. The work in progress changes can be made safely while the system is using the original implementations. In order to demonstrate the new functionality, rely on automated tests or wire up the new version. Once fully integrated and tested, simply remove the old implementation. The addition or removal of implementations acts as the toggle in this case.

To extend the SimpleReceiptWriter a new version is made. This work in progress implementation has no limit on the time to complete. The new implementation will only take effect once configured.

Configuration takes the form of composition root or dependency injection container changes. Given your code does not know the concrete implementation (apart from tests) you should be fine to make these switches.

If no abstraction exists you can introduce one if valid. If no valid abstraction exists you can simply fallback to feature toggles.

Branch by Abstraction plays nicely with Walking Skeletons. Your first implementation will most likely be a simple first pass attempt. Overtime these can be replaced with more fleshed out versions.

Anytime you feel the need to create a branch in source, Branch by Abstraction can be a valid alternate in most cases. UI changes are better suited as Feature Toggles due to the nature of the code.

Tuesday, 2 June 2015

Feature Toggles

I'm a fan of regular releasing. My background and experience leads me to release as regularly as possible. There are numerous benefits to regular releases; limited risk, slicker release processes and the ability to change as requirements evolve.

The problem with this concept is how can you release when features are not functionally complete?


If there is still work in progress, one solution to allow frequent releases is to use feature toggles. Feature toggles are simple conditional statements that are either enabled or disabled based on some condition.

This simple example shows a feature toggle for an "Edit User" feature. If the boolean condition is false, then we only show the "New User" feature and the "Admin" feature. This boolean value will be provided by various means, usually a configuration file. This means at certain points we can change this value in order to demonstrate the "Edit User" functionality. Our demo environment could have this enabled, while the live system would be disabled until the feature is fully complete.

If the feature to edit users took more than an ideal release cycle the code could still be released. As long as all the tests and other release checks pass there is no reason to defer this task. This is after all one of the benefits of continuous integration. Any consumer of this code base would always be working with up to date code, merge conflicts would be next to non existent. Our new code would be integrated regularly.

Ideally feature toggles live as high as possible in the dependency graph of your application. In most cases this would be the composition root of the application or within UI/presentation logic. This simplifies the addition of toggles, but you need to be careful that just because the UI hides a feature it is not truly disabled. In scenarios where security is a concern the feature toggles may need to live further down the stack.

It's best to remove feature toggles once the feature is complete otherwise they can become a maintenance burden. Is this feature enabled or disabled? Can we delete this code? These sort of questions can cause legacy code to live unquestioned. One way to aid in their removal is to add assertions to fail the build at a certain point in the future or include a toggle with built in date/time logic.

Feature Toggles help with demonstrating features, but they can be more complex. For risky features you may want to slowly ramp up the number of users who are exposed to the feature. In this case the actual toggle may perform some basic logic such as "one out of ten requests" enable the new feature. Overtime this ratio can be increased until the feature is fully enabled and proven.

Another technique to allow fast, regular releases is to rely on Branch by Abstraction. This works great when the toggles live in the composition root or the team have the ability to split work around features.