Creating a maintainable, flexible codebase is not easy but is an essential part of software engineering. In this series we’ll take a look at a simple, functional weather app and look at some of the issues in its design. We shall then refactor and re-design it to create a codebase which will be easier to maintain, less prone to bugs, and easier to add features to.
Weather Station is a simple weather reporting app which displays the most recently observed weather data at the current location of the device. It uses the OpenWeatherApp API to obtain the weather data, retrieves it using Retrofit, and decodes the JSON using Moshi. The app is actually pretty stable and well behaved (for example, it cancels any outstanding Retrofit calls at an appropriate point in the Fragment lifecycle) and it is easy to conclude that the code is well organised and maintainable. However, that is not, in fact, the case and there is an awful lot that we can do to improve things.
The code consists of an Application instance which is used to initialise the ThreeTenABP library which is used for date time handling; A single Activity which handles obtaining runtime permissions for Location, and is the host for three separate Fragments; The first Fragment (
NoPermissionFragment) will be displayed when the Location permission has not been granted; the second (
CurrentWeatherFragment) displays the current weather data; and the third (
PreferencesFragment) is a Preferences page which allows the user to specify the desired units for wind speed and temperature. Then there are some data classes (in the
com.stylingandroid.weatherstation.model package) which are how the weather data gets represented as JVM objects, and the Retrofit HTTP API interface in
com.stylingandroid.weatherstation.OpenWeatherMap. Finally there is a
Converter class which takes a numeric value and converts it to a string using the units that the user has specified in preferences.
The big problem area is CurrentWeatherFragment. While this isn’t a huge class (it’s only around 200 lines of code including imports), there is instantly a smell about it before the class declaration begins:
Just the sheer number of imports is an issue – it indicates that this Fragment has many collaborators. It is also with considering the package name – it identifies that this is part of the UI, yet the imports suggest that it is touching lots of other areas as well – such as Retrofit (which is handling the network call), and the location provider. We would expect it to only have access to the actual data produced by those, and not collaborate with them directly. As it stands this is a clear violation of the single responsibility principle (part of SOLID), and also we do not have a good separation of concerns. While this may not appear to be an issue in a small app such as Weather Station, we will quickly start running in to problems if we start adding features. For example, perhaps we may want to add the 5-day forecast which is also available from OpenWeatherMap, then we would need to start reproducing the code from here to wherever we obtain the 5-day forecast, which may be in a different Fragment.
Another problem here is that there is no degree of abstraction from these collaborators. What if we wanted to move to a different weather data provider? By having this logic inside all of our Fragments it is a major undertaking to replace all of these to use the new provider. Or alternatively, we may want to switch from Retrofit to another HTTP client library because new features demand it – once again this would be difficult because of the close collaboration with Retrofit in this Fragment. And these complexities will only get worse as our codebase grows – and we have lots of replicated code, which is tightly-coupled to Retrofit and OpenWeatherMap, and replacing either or both of these would become a major undertaking.
That is not the only concern here. Although the app works, it is actually pretty inefficient because it will request the data each time the Fragment is loaded, including when the device is rotated causing the Activity and Fragment to be re-created. It will also retrieve new data when the user goes to the settings page and then returns to the current forecast page. Constantly retrieving new data in this way forces the user to wait while new data is retrieved, but it is also consuming network bandwidth and battery which is bad news for the user. It may also be making lots of API calls and if the service we are using has limits (the free OpenWeatherMap API permits no more than 60 API calls per minute) then we may find that multiple users using the app results in our API call limit being consumed extremely quickly.
Those familiar with Retrofit / OkHttp may think that enabling a local cache may overcome this issue by serving up cached data. However the OpenWeatherMap API does not include eTag or expiry headers in its responses, so conditional GETs are not supported. A local cache will only work unaided if the server supports conditional GET requests. To protect against this I have implemented a local cache with a 10 minute expiry which is specified using a cache-control header.
A further issue here is how the navigation within the app is organised. Each Fragment is responsible for handling the navigation when it is active. Once again it will become more and more complicated by having this navigation logic dotted throughout the codebase, and is likely to result in inconsistent behaviour throughout the app – again the user will suffer as a result.
The other really important thing to bear in mind that much of our app logic is bundled in here and this class is really difficult to test. It is responsible for creating the Retrofit instance used to retrieve the data, so that is difficult to mock using this design. There is one small part of the app which is designed much better – the Converter class. Although it is really quite small and simple, it provides a lot of power in a small package because it removes and need for CurrentWeatherFragment to touch SharedPreferences, or even be concerned about the units that the user has selected as their preference. Moreover, the only real collaborator is the SharedPreferences instance, and this is obtained via a default constructor argument. This allows us to substitute in a mock SharedPreferences instance and therefore permits us to create a test suite with good coverage
So the problems are many, but in a simple app such as this they are pretty easy to resolve, and we’ll tackle this using a variety of techniques and we progress. In the next article we’ll look at how we can begin fixing things up.
The source code for this article is available here.
Maintainable Architecture – Introduction by Styling Android is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Permissions beyond the scope of this license may be available at http://blog.stylingandroid.com/license-information.