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

  1. 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.

  2. 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() and hashCode()), @Parcelize (implement Parcelable), Room (database queries)

  3. Prefer composition over inheritance. Otherwise you’ll end up, sooner or later, with child classes that inherit a lot of unnecessary code.

  4. An interface with just one implementation points to either over-engineering of the code or an incomplete refactoring.

  5. Avoid magic constants.

  6. Make sure things are named properly. If you have both a class called SyncService and an interface called SyncService, and the class does not even implement that interface, things get confusing very quickly.

  7. 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

  1. 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.

  2. Do not use general data-heavy API endpoints. Use specialized API’s to avoid transferring, parsing and storing useless data.

  3. Keep business logic as much as possible on the server.

Android Tips

  1. Do not call somethings a Service unless it really is an Android Service. Existing Android terminology implies certain behavior and expectations.

  2. Never do string concatenation in code. Use placeholders in Android string resources instead.

  3. Avoid tight coupling of activities and fragments. Use a shared ViewModel to exchange data and events.

  4. Manage fragments properly. Let the OS do its job, don’t fight the framework. If your activity has methods called pushFragment(), popFragment() and moveToFragment() it sounds like your are rewriting core Android functionality.

  5. 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.

  6. Don’t reinvent the wheel. The Android Architecture Components contain plenty of useful libraries.

  7. Embrace Kotlin!

Updated: