Introduction to Clean Architecture
Introduction to Clean Architecture
“Any fool can write code that a computer can understand. Good programmers write code that humans can understand.”
– Martin Fowler
Architecture relates to the way we build and design our mobile apps. It is often not enough to simply have something work, but also ensure that the code that is written is of good quality as well. We typically refer to such code as “clean code”, and we often say that when an architecture is well built, it is build with a “clean architecture”.
But what does “clean code” actually mean and how can we achieve it? We can start by identifying some key aspects of it:
1. Code is clean when it is easily readable and easily understandable.
Beginners often believe that the more complex, smart-looking or short the code is - the better. This is a misconception. In real life, we often work with a team of other developers. Thus, it is incredibly important that the code that we write can be easily understood by others.
2. Code is clean when it works the way you expect it to work.
We’ll get back to this topic, but the basic idea is that code must do exactly what it says and nothing else. If you have a method to fetch movies from an server, but for some reason it also does some persistence logic and updates the UI, this is not good.
3. Code is clean when it follows the same coding principles and standards.
Real life applications are the result of tens of thousands and even million lines of code. The bigger the codebase gets, the more crucial it is that we keep the same standards across our application. Otherwise, it becomes much more complex to read and maintain properly.
4. A clean application architecture is one that is consistent.
Sometimes, it is not as crucial which architecture you pick for your application, but rather how consistent you are with your selected decision. If I’m developing the “Login” feature of my app, and eventually switch to the “Settings” feature, I expect it to look and work exactly the same way.
If one part of the application uses callbacks to send data, another part of the app uses NotificationCenter
instead, and maybe in a different part we use delegates, our application will quickly become very difficult to maintain.
5. A clean application architecture is one that is flexible.
Real life applications evolve over time. New features are introduced, others are updated and some are removed. Our codebase and our application constantly evolves and grows over time.
As a result, it is crucial that we design our applications in a way that encourages flexibility. We should be able to easily add new features, swap old implementations with newer ones, and remove things without fear of breaking the entire thing.
If you feel overwhelmed, that’s fine. I’ve enumerated 5 that were at the top of my head, but if I were to build a comprehensive list, I’m sure the list would be much, much larger.
At this point, it is important to not get too worked up about this. These are principles that take time and patience to master. The more code you write the better you become at identifying what is “good” and “bad” code.
For now, let’s start exploring how we can improve our applications by applying some of these principles described above.
🧑🎓 Self-Assessment:
Let’s start with some self-assessment questions. Take your time and feel free to answer them as best as you can: