oliverdavies.uk/source/_daily_emails/2024-03-04.md

1.4 KiB

title date permalink tags cta snippet
Why you need layers in your application code 2024-03-04 daily/2024/03/04/why-you-need-layers-in-your-application-code
software-development
software-architecture
clean-code
~ Why should you make your code more robust and flexible by introducing layers into your code?

You need to add an integration with a new service or supplier to your application code.

You should avoid writing code that interacts directly with that service or supplier - a.k.a tightly-coupled code.

What if you change to a different service or supplier in the future?

What if they release a new version of their API that includes breaking changes?

If your code is tightly coupled to a single implementation, you'd need to rewrite the code, and there wouldn't be a way to easily switch from the old version to the new one.

By introducing an adapter layer, you make your code more loosely coupled.

You can have multiple implementations, with one for each different supplier, service, or version.

You can have a consistent interface and API for every implementation, making them hot-swappable and making it easy to switch between different implementations.

If you change supplier or they release a new version, you write a new implementation for it and switch when you're ready.

Also, as every implementation satisfies a common interface, it's easy to make a fake implementation that you can use for testing.