Clean Architecture: Layers and Boundaries
Tags: architecture, cleanarchitecture, tutorial, performance
Published: 2022-06-23 17:26:20

After understanding about the key concepts of [Clean Architecture](https://dev.to/rubemfsv/clean-architecture-the-concept-behind-the-code-52do) and some important concepts that surround it such as [SOLID](https://dev.to/rubemfsv/solid-the-concept-behind-the-code-3h4o) and [component principles](https://dev.to/rubemfsv/component-principles-the-concept-behind-the-code-dn6), it is easier to understand about its division into layers and its boundaries.

A system can be defined by three basic components: the UI, the business rules and the database - and in larger systems there are many more components. To explain the concept of layers and boundaries, the example made by MARTIN (2017) will be used, which describes the game of the 1970s called Hunt the Wumpus.

The rules of the game are irrelevant for this example, as the focus will be on the implementation itself.

Considering that a text-based UI is used, it is necessary to decouple the game rules so it can have different languages. The rules will communicate with the UI using an API, regardless of language, and the UI will translate to the requested language. In this way all UI components will be able to make use of the same rules of the game. Assuming that the game state is kept in a persistent store, there is no need for the game rules to know the details, so an API will be created to communicate these rules with the data store component.

The game rules must also not know about the different types of data storage, so the dependencies must be correctly addressed following the Dependency Rule, as seen in the game's basic structure figure (source: MARTIN (2017)).

![Game's basic structure](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zkb1zyaakrqggjyszhk6.png)

After this brief description and based on [Clean Architecture concepts](https://dev.to/rubemfsv/clean-architecture-the-concept-behind-the-code-52do), it is imagined that clean architecture would be easily applied in this example. However, not all architectural boundaries are clear.

In this example, the language is not the only UI change axis, the communication mechanism can also be varied, such as a chat application, a normal window or text messages. There are countless possibilities for change, which shows that there is an architectural limit defined by these axes of change. Therefore, it is necessary to create an API to cross this limit and isolate the language from the communication mechanism.

![Reformulated game structure](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t5omobpp5pbtjda810m6.png)

The figure above reflects the reformulated game structure (source: MARTIN (2017)), the dotted rectangles are the APIs and the rectangle written "Game Rules" contains the game rules. The Rules of the Game component is positioned at the top of the diagram, as it contains the highest level policies. This organization divides the data flow, being the left side to communicate with the user and the right side for data persistence. As systems grow, the component structure can be divided into several flows with different hierarchies, which makes the architecture increasingly complex.

MARTIN (2017) concludes this issue by stating that the role of the software architect is to assess costs, determine architectural limits and which of these limits should be fully implemented, which should be partially implemented and which should be ignored.

## Conclusion

This simple example is meant to show that there are architectural boundaries everywhere, and it's up to software architects to recognize the needs.

It is also necessary to know that implementing these boundaries is something expensive, but at the same time, if not implemented early in development, they can have even greater costs over time.

Martin reinforces that these decisions are not made just once, that it is necessary to observe the evolution of the system, analyze where boundaries may be necessary and what can happen if they do not have well-defined boundaries. He also reinforces the importance of comparing the costs of implementing these boundaries with the cost of ignoring them, always looking to implement the right boundaries at the sweet spot where the cost of implementation is less than the cost of ignoring them.

## References

* MARTIN, Robert C. Clean Architecture: A Craftsman’s Guide to Software Structure and Design. 1st. ed. USA: Prentice Hall Press, 2017. ISBN 0134494164.