As a freelancer you get to work on different types of apps for different customers. Sometimes you can build a brand new app from scratch, but often you are called in to work on an existing app.
Recently I was hired by a company to help them clean up and rearchitect the 5-year-old codebase of their Android app. Here are some tips, in no particular order, based on that experience.
General Tips
-
If a piece of code or resource is no longer used, throw it away. Both Android Studio and Lint help you to detect this. Unused resources slow down your builds.
-
Proactively reduce boilerplate code. Repetitive low-level code can often be replaced with solid robust 3rd party libraries like ButterKnife (view injection), GSON or Moshi (JSON parsing), Kotlin data classes (implement
equals()
andhashCode()
), @Parcelize (implement Parcelable), Room (database queries) -
Prefer composition over inheritance. Otherwise you’ll end up, sooner or later, with child classes that inherit a lot of unnecessary code.
-
An interface with just one implementation points to either over-engineering of the code or an incomplete refactoring.
-
Avoid magic constants.
-
Make sure things are named properly. If you have both a class called
SyncService
and an interface calledSyncService
, and the class does not even implement that interface, things get confusing very quickly. -
Avoid flags/parameters/settings to make a product configurable. This might seem flexible but it is impossible to maintain in the long run. It turns a standard product into a custom/unique implementation for each customer. Nobody ever tests all combinations of flags/parameters/settings anyway.
Mobile Tips
-
Avoid bringing resource-heavy server-side software development patterns to mobile. Battery capacity is limited! For example, not everything belongs in a database. If the data set is small and does not need to be joined with other data, then an in-memory copy, backed by a JSON on disk, is sufficient.
-
Do not use general data-heavy API endpoints. Use specialized API’s to avoid transferring, parsing and storing useless data.
-
Keep business logic as much as possible on the server.
Android Tips
-
Do not call somethings a Service unless it really is an Android
Service
. Existing Android terminology implies certain behavior and expectations. -
Never do string concatenation in code. Use placeholders in Android string resources instead.
-
Avoid tight coupling of activities and fragments. Use a shared
ViewModel
to exchange data and events. -
Manage fragments properly. Let the OS do its job, don’t fight the framework. If your activity has methods called
pushFragment()
,popFragment()
andmoveToFragment()
it sounds like your are rewriting core Android functionality. -
Some standard Android components are not worth using because they are error-prone (AsyncTask) or over-engineered (IntentService). Follow the Android community for good alternatives.
-
Don’t reinvent the wheel. The Android Architecture Components contain plenty of useful libraries.
-
Embrace Kotlin!