In the tech world, whenever we hear the word ‘architecture’, 80% of us start thinking about coding patterns such as MVP, MVVM, MVI, and similar.
Yes, these patterns are a part of ‘architecture’ but there’s a lot more that contributes to building a clean & maintainable application.
I’m writing this article to share my experience & what I’ve learned (in my 5 years of coding journey) about building a solid app architecture. This is a theoretical article so please bear with me.
Understanding Responsibility as a Developer
Being a developer is not only about building & delivering what you’re asked for. It is more about providing scalable, maintainable & optimised solutions that are easy to understand & robust enough to serve for decades.
Developing is an art and it should be reflected in each line of code by following proper standards. A developer should be true & committed to every function he is developing and not just focus on the outputs of a program.
Many-a-times because of delivery deadlines, we’re forced to code quickly. By doing this, many rules of architecture tend to get violated.
In this case, a developer should come back to such code and fix all such violations in his spare time.
All the people associated with the project, specially the client & the project manager, should know the importance of architecture in building an app.
Many clients find it time-consuming and focus more on feature development (because that’s where the money comes from). This is wrong! An app with more features and poor architecture would be profitable for a short while but eventually, it would fail terribly.
Speaking from my experience in one of my “poorly architectured” projects
To build a new feature, I was forced to ask for 3 weeks more time just to refactor my code so that it can be capable enough to add the requested feature.
This happened even though I followed a good coding pattern since the beginning of the project.
Lack of time is the main reason for it… due to many new features & change requests & absence of proper architecture.
A developer must “fight” for buying enough time to build a strong base using good architecture. This would eventually be a time-saver in the future.
After understanding project requirements, many of us skip many steps and dive directly into coding. One of those skipped steps is visualization.
Visualizing helps us prepare a map of classes and establish relationships between them. This guides us in modularising the app (which is the next step) for decoupling & delegation.
UML class diagrams are used to prepare code visualization maps. This can be done using merely a pen & paper. Also, there are many online tools available to build these diagrams.
Modularisations means dividing the app into smaller modules for :
- Code decoupling
- Easy task delegation
- Faster build speed
- Unit testing
- Easy debugging
Many developers believe that“less coding is good coding”. They create the least possible classes/files and write too compact code. They aim to produce an output with the least possible size and hence they avoid extra code generation such as data binding and creating separate modules.
I agree that output size matters. but not more than a decoupled, readable code. Nowadays internet data is cheaper and people don’t care about spending a few more megabytes on an app unlike they did before.
A monolithic architecture not only slows down the compilation but also the developer himself. It becomes hard to maintain a clear flow of control and to keep a boundary between classes.
In my previously discussed project, a monolithic app generally takes around 8 to 10 minutes to build the APK.
There is no perfect approach to modularizing the app. This depends on the scope of the app. This video by Mitch Tabian is very helpful to understand various approaches to modularisation.
Coding architectures or coding patterns such as MVP, MVVM, MVI are extensions of clean architecture. Clean architecture is a bundle of organizing principles that helps us to establish rules that must be followed to build a scalable app.
I don’t think I need to talk more about coding patterns because the Android team from Google is doing a fantastic job in spreading awareness & importance of good coding architecture through such posts.
But I would like to recommend reading Uncle Bob’s Clean Architecture book & this book wherein Petros Efthymiou has explained clean architecture from a mobile perspective.
Here are some SOLID principles that should be followed in literally every software that is being built.
Coding is an Art
Online discussion forum platforms such as StackOverflow help us most of the time when we get stuck. Many of us (focusing just on the output) copy-paste the code from SO. Due to this, two types of code smells can be introduced in our code i.e. unused variables & improper nomenclature.
Coding is nothing less than a piece of art. It should communicate its purpose by merely looking at it. To achieve that, proper naming conventions should be followed. Function & variable names should be self-explanatory.
Don’t Think Too Much
Scalability-centric development approaches tend to make our thinking very generic. Usually we don’t only fulfill current requirements, but also start thinking about future change requests and code accordingly.
It is very much possible that the “future change request” would not be raised or even worse — the feature itself is now removed from the app.
For instance, making a generic function that can accept input as various data types but only String was the original requirement.
Hence, we must write code that satisfies only the current requirements and should be flexible enough to scale with minimal changes.
Technology is changing every day. There are solutions for every challenging requirement. The architecture may need a little change. For instance, MVI architecture is fairly new when compared with other MVX versions.
Hence, developers, who have found a perfect architecture for their needs, should keep their minds open to changing their architectures & approaches with time. They shouldn’t be rigid and stick to their “perfect” architectures which might no longer be perfect.
Lastly, I would like to recommend Refactoring Guru which is a wonderfully designed website to understand various concepts and patterns that are related to coding architectures.