How did I grow to think about code architecture in Android?

Chirag Jain
4 min readApr 9, 2017

Initially, I was thinking about directly explaining the architecture which I had described in my previous article. But after completing the article, I shared it with some fellow developers and I realized that all of them were keen to learn but only few understood it’s purpose. Few of them, who had less industry experience, thought it was just about writing code from one file to another. So I wanted to share little bit of my experience before explaining the architecture.

After my graduation, I started my career as a front-end developer in Android for my love in designing and interest in mobile technology. In my initial days of development I used Activity class where I wrote all the shit code.

Now I want to explain the above things with an analogy of a company. Assume that I am an owner of a company “Search My Movie Android App” and have only one employee “Miss Activity” who does everything.

I started my company and hired my first faithful employee Miss SearchMovieActivity to handle all the responsibilities. Initially for few days she worked perfectly. She would take the request from users to search movies and then if she finds it on internet, she would show them to the user otherwise tell them that “No movies found”. Everything was working smoothly but one day I realized that my company was not growing or scaling. So, I wanted to search for songs also. Then I told SearchMovieActivity to search for movies and songs. She started following it. But one day a customer filed a complaint that there is a movie named Inception not showing in the results. I got angry and yelled at SearchMovieActivity for being irresponsible. It took around two days to find the problem and a day more to show it in the results. Soon I realized there was too much work for SearchMovieActivity. So I hired two more employees MovieDataManager and SongDataManager and told them to manage the data of movies and songs respectively. Now SearchMovieActivity need not to go to the internet to fetch data. She directly asks for data from both the managers. As the company grew, I added support for images and tv-series.

Life after Managers

Life was good till one day a customer gave me a suggestion that the way we are showing images is not great. So I told SearchMovieActivity to show them in a better way. Being a loyal employee to the company she did it. But then again after few days it was not showing results for some keywords. I again yelled at SearchMovieActivity and told her to fix it. She again took 5 days to find out the issue and two more to fix it. I soon felt that instead of rewarding her with perks , I was just putting pressure on her . Then I hired two more employees to work directly under SearchMovieActivity, one is ResultGenerator and SearchDataHelper. Now SearchMovieActivity only had one responsibility of showing data. Whenever user query came, she asked SearchDataHelper to search for the data. SearchDataHelper went to different managers and collected the data from them and gave it to SearchMovieActivity. SearchMovieActivity then passed the data to ResultGenerator and then he transformed the data in required format and gave it back to SearchMovieActivity. From then every thing is working smoothly and became scalable.

Single Responsibility Principle by Uncle Bob which states,

A class should have only one reason to change.

Whenever we talk about architecture, most of the time it comes down to segregation of responsibility. Above story can tell you, how I achieved the behavior of segregation.

This story revolves around a small company, what if you want to create and establish a large company. We actually looked at the processes different companies follow and then chose the best one for self. So, MVP, MVVM, MVC, VIPER etc. are nothing but the processes running in old organizations and developed by very experienced guys out there.

Similarly, Unit Testing or Testable code is also very important part of software architecture as you would be able to foresee the problems before any customer complains.

This article might seem very obvious but this very well summaries how I understood the importance of a good architecture.

--

--

Chirag Jain

Engineering Manager, Android Application Developer