Skip to content

Coding guidelines

kautilya edited this page Apr 23, 2018 · 9 revisions

Project structure

File naming

  • Class Naming: Class names are written in ​UpperCamelCase

    ​SignInActivity,​SignInFragment

  • Class Type: mention type of class in name like:

    • Button-

      btn_send_pressed.png

    • Adapters -

      LocationSearchAdapter, AddDataSetListener

    • Interface/Callbacks -

      OnLocationSetListener

    • Utils class -

      ValidationUtils, FirebaseUtils

  • Resource files mention type of resource as prefix like:

    • Button-

      btn_send_pressed.png

    • Icons-

      ic_star.png

    • Launcher icons-

      ic_launcher_calendar.png

    • Menu-

      menu_submenu_bg.png

    • Selector state- ICONNAME_STATENAME like

      btn_order_pressed.png

  • Layout files mention Type of components where they are being used with its usage like:

    • Activity

      activity_user_profile.xml

    • Dialog

      dialog_change_password.xml

    • Adapters Item

      item_contacts.xml

    • Toolbar

      toolbar_custom.xml

Java codes

Variable Declaration

  • Member variables Naming: variables name start with m
    • String name ​ : mStrName;
    • TexView textViewName : ​mTvName;
  • FINAL variables Naming: variables name in UPPER_CASE

    public ​​static ​​final​​ int ​​SOME​_CONSTANT​=​​42​;

  • Android constants/keys naming with prefix:
    • SharedPreferences PREF_
    • Bundle BUNDLE_
    • Intent Extra EXTRA_

Logical codes

  • Don't ignore exceptions

    void​ setServerPort(String value) {
       try​​{
          serverPort =​​Integer.​parseInt(value);​
       }catch​​(NumberFormatException e){ 
          // Appropriate Message to user or error handling 
       }
    }
    
  • Don't catch generic exception

    try​{
       someComplicatedIOFunction();   ​    // may throw IOException
       someComplicatedParsingFunction();  ​// may throw ParsingException
       someComplicatedSecurityFunction(); //​ may throw SecurityException
                                      
    } catch​​(Exception e) {	              //​ I'll just catch all exceptions
       handleError();	              //​ with one generic handler!
    }
    
  • Braces standard braces alignment

    public void method() 
    {
        if (condition()) {
          try {
            something();
          } catch (ProblemException e) {
            recover();
          }
        } else if (otherCondition()) {
          somethingElse();
        } else {
          lastThing();
        }
    }
    
  • Mention access specifier

    private​​ int ​​mTotal​=​​0​;

  • Activity life-cycle order

    public ​class ​​MainActivity ​​extends​​ Activity​​{
     //Order matches Activity lifecycle
    
    @Override​
    public​​ void​​ onCreate​()​ {}
    
    @Override​
    public​​ void​​ onResume​()​ {}
    
    @Override​
    public ​​void ​​onPause​()​ {}
    
    @Override​
    public​​ void​​ onDestroy​()​ {}
    
    }
    
  • App Global Constants

    • Use AppConstants.java for constant/final values

      public class AppConstants {
         /*API SERVER URL*/
         public static String BASE_URL="http://server.com/api/v1/";
         public static String SIGNUP_URL=BASE_URL+"register";
      
         public static String CONTENT_TYPE="application/json; charset=utf-8";
          /*JSON Parsing keys*/
         public static String KEY_PHONE="phone";
         public static String KEY_NAME="name";
      }
      
    • Avoid writing each line of code within onCreate() or any function divide it in functions

      @Override
      protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_profile);
         findAllViews();
         initialize();
         getProfileData(userId);
      }
      
    • Make app functionality in modules

    • Create and use requestCode and resultCode for onActivityResult() e.g

      private int final REQUEST_CAMERA=1;

    • Use InBuild resultCode

      Activity.RESULT_OK

    • And in onRequestPermissionsResult()

      private int final REQUEST_STORAGE_PERMISSION=1;

  • Use Java and Android API’s e.g

 ArrayList<String> nameList;
 Collections.sort(nameList);
 Collections.binarySearch(nameList,"David");
 Collections.fill(nameList,"N/A");

XML codes

  • **Use self closing tags **
<TextView​
android:id="@+id/text​_view_profile"
android:layout_width="wrap​_content"
android:layout_height="wrap​_content"​/>
  • **ID naming **

    android:id="@+id/imgProfile"

Espresso tests

  • Every Espresso test class usually targets an Activity, therefore the name should match the name of the targeted Activity followed by ​Test,.

    SignInActivityTest

Clone this wiki locally