in Coding

Don’t depend on details – an example

I recently read Clean Architecture by Robert C. Martin. The book gives a simple advice: don’t depend on details! For Uncle Bob details are things like the database or message broker. Your architecture shouldn’t depend on the actual used implementation of those details. Whether your are using Oracle, MySQL or DynamoDB, whether it’s ActiveMQ, Kinesis or an in-memory Guava event bus – your architecture shouldn’t be affected.

Don’t depending on details has some advantages:

  • As you don’t depend on the actual implementation, you can change it. You can decide to use DynamoDB instead of MySQL because you want to migrate to AWS.
  • You can test your code more easily. As you don’t depend on an actual implementation you can use mocks or an in-memory variant for testing or local development.
  • You can change your mind. Being independent gives you the freedom to change things in the future.

Today I updated one of my demo projects on GitHub. I replaced Guava’s event bus with Spring’s own event bus implementation. Both implementations do exactly the same – they send events. However, my code had a strong dependency to Guava. Each and every listener imported an annotation from the Guava library. A detail on which I depended.

The result was this pull request:

(https://github.com/bringmeister/ddd-with-kotlin/pull/2/files)

Although the implementation of Guava and Spring are doing exactly the same, I needed to change 18 files. There were no tests and it’s just a small and stupid demo project – still, I needed to touch 18 files in order to switch from one in-memory message broker to another. This shouldn’t be the case! Don’t depend on details!

Best regards,
Thomas

More

  • https://8thlight.com/blog/uncle-bob/2012/08/13/the-clean-architecture.html

Write a Comment

Comment

  1. Hello Thomas,
    Thank you for your post.

    Have you implemented any improvement?
    Do you have any idea, how could you do it better to avoid this “not depending on details”?

    Actually, you haven’t changed any code, only metadata (annotations) :).
    If instead annotations you had used good old xml configuration, you would had to change only one xml file:)

    I don’t know if guava allows replacing annotations with xml configuration, similarly I don’t know if it’s possible with spring at least without implementing some spring specific interface. Assuming it were possible, would you prefer it?