Android Coding Guidelines

Maqsood Khan
3 min readOct 31, 2019

--

In this article i am explaining about the coding guidelines

#1. Follow the basic coding standard as per the developer.android.com

#2. Android Architecture Components

A collection of libraries that help you design robust, testable, and maintainable apps.

Checkout the Official documentation.

Room
Official documentation
Article on how to implement Room Db
Sample implementation

Live Data
Official documentation
Sample implementation

ViewModel
Official documentation
Sample implementation

Data Binding
Official documentation
Sample implementation

Lifecycles
Official documentation

Difference between MVC & MVP & MVVM

MVC is the Model-View-Controller architecture where model refers to the data model classes. The view refers to the xml files and the controller handles the business logic. The issue with this architecture is unit testing. The model can be easily tested since it is not tied to anything. The controller is tightly coupled with the android apis making it difficult to unit test. Modularity & flexibility is a problem since the view and the controller are tightly coupled. If we change the view, the controller logic should also be changed. Maintenance is also an issues.

MVP architecture: Model-View-Presenter architecture. The View includes the xml and the activity/fragment classes. So the activity would ideally implement a view interface making it easier for unit testing (since this will work without a view).
Sample Implementation

MVVM: Model-View-ViewModel Architecture. The Model comprises data, tools for data processing, business logic. The View Model is responsible for wrapping the model data and preparing the data for the view. IT also provides a hook to pass events from the view to the model.
Sample Implementation

#3. If you are following MVVM/MVP architecture it doesn’t mean your code is quality, its about how are you writing the code, please follow the below steps :

  1. Avoid creating unnecessary objects

Example :

void funFoo(ArrayList<Student> list){

for(int i=0; i<list.size(); i++){
Student obj = list.get(i); // Here creating extra unnecessary object while iterating loop instead of taking directly from list
System.out.println(obj.id);
System.out.println(obj.name);
}
}

2. Initialise your List w.r.t requirement

Example : consider you know the capacity is 100 or more size, you initialise your list to 100 to avoid calling internally grow capacity method multiple times, since the default bucket size is 10 initially

1. ArrayList<String> list = new ArrayList<>(100); // correct 2. ArrayList<String> list = new ArrayList<>(); // wrong w.r.t requirement since the default bucket size is 10 

3. Use StringBuilder for appending multiple strings, for threads safe you use StringBufferer instead of String to avoid creating unnecessary objects

4. Always include unit tests. This is the most important part of application development. I recommend running unit tests on the JVM, because it’s much faster than running them on the Android device itself or an emulator. If you require any android dependencies, use Robolectric. If you need to mock some objects while testing, use Mockito.

Use Dependency Injector to make testing easier. Learn how to use dependency injector from here and here.

Always include functional UI tests. Functional tests check the functionality of your app from the user’s point of view. They launch your app and test its functionality. Here you can use Android Instrumentation if your application is not going to interact with other applications, as it runs only with your application. If there is an interaction with other apps use UIAutomator for testing this functionality.

5.Use Proguard in your release version. This will remove all your unused code, which will reduce APK size.

6.Use the Parcelable class instead of Serializable when passing data in Intents or Bundles. Serialization of an object that implements the Parcelable interface is much faster than using Java’s default serialization which creates unnecessary objects . A class that implements the Serializable interface is marked as serializable, and Java serializes it using reflection (which makes it slow). When using the Parcelable interface, the whole object doesn’t get serialized automatically. Rather, you can selectively add data from the object to a Parcel using which the object is later deserialized.

7. File operations should always be performed on a worker thread

8. Usage of Context Learn from here.

9. Avoid memory leak Learn from here

Also, Let’s become friends on Linkedin & Github

--

--