Best Android Practices to make an optimised and easy to understand Android application development

1.  Name variables efficiently

         Naming variables as ‘a’, ‘I’,’d’ proves to be a great headache during debugging. It is advised that the names of variable should indicate their usage.
Example -  Button cancelButton, saveButton; proves to a saver while debugging, instead of Button c, s;
Even Button cancel, save; is not recommended, as you may not know whether it was a Button or
TextView, unless you search for it. CamelCase format for naming is recommended. In camelcase first
word starts with small letter and consecutive words start with capital letter with no spaces in between.

2. Modularising codes

          Projects can have 100 – 150 classes. Those include Activities, Fragments, Adapters, Custom Views, etc. To make things easier, directories named as ‘Activities’, ‘Fragments’, ‘CustomViews’ etc. can be created inside the java folder of a project. Below project screenshot depicts one such view
Modularising Code 

3. Declaring constants at one Java class

          User-defined functions that are used in various parts of the functions can be declared at one place. This will reduce size of the project and help in easy modifications. Strings that are used as Tag names can also be declared here. The data type of just strings will be ‘public static final String’.
Declaring Constants in One Class
4. Use the latest compileSdkVersion and targetSdkVersion

        It is recommended to make android apps that are compatible to the newest versions. This reduces chances of crashes in the production app.

5. Following the conventions for naming Java classes and xml layout

       The first letter of a class name should always be capital. If the name is composed of 2-3 words, then the first letter of each word should be capital. Examples of some class names are SignUpActivity, TransactionHistoryFragment. For naming layout files, the first word should indicate the type of layout: an activity (activity_), a fragment (fragment_), a listview item layout (layout_item_), a selector (selector_).
These help in proper organisation of files.

7. Prevent use of hard-coded values

         Define the values of strings, dimensions, colours in the respective xml files available in the values folder. Change of values is made easier when these are defined once (i.e. in the values folder xml files) and reused later.



<dimen name="padding_main">30dp</dimen>
Prevent use of hard-coded values
8. Comment your code appropriately
        Always add comments before a function or class declaration about its functionalities. Such practises make it easier to recall the purpose of the function and increase its readability. Make use of the beautiful feature of Android Studio to add TODO lists. All TODOs in the project are highlighted in blue colour. These are very useful when certain tasks are postponed to be accomplished later.

Function to change the format of dates. Date returned in the format dd/mm/yyyy.

public static String changeDateFormat(int date, int month, int year) {
String d = "", m = "";
String cDate = "";
//TODO: implement logic to set dates in the correct format
return cDate;

9. Do use try catch block to catch all exceptions in one go
         Catching all exceptions at once using the Exception class may prevent your application from crashing but will handicap it. You will never know why a particular feature is not working. Use the child of Exception class instead. This will let you know why exactly the application in production stopped at a point. The logs are obtained in the crashes section in Google Play Developers Console.

try {
JSONObject jsonObject = new JSONObject(data);
Boolean success = jsonObject.getBoolean("success");
if (success) {
JSONArray jsonArray = jsonObject.getJSONArray("transaction_logs");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject tId = jsonArray.getJSONObject(i);
String merchant = tId.optString("merchant_name");
} catch (JSONException e) {
} catch (NullPointerException e){
Hope these Android best practices for beginners will help you to code a better Android project / Android application..



Post a Comment