-
Notifications
You must be signed in to change notification settings - Fork 123
Coding guidelines
-
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
-
-
Member variables Naming: variables name start with
m- String name :
mStrName; - TexView textViewName :
mTvName;
- String name :
-
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_
- SharedPreferences
-
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.javafor constant/final valuespublic 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
requestCodeandresultCodeforonActivityResult()e.gprivate 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");
- **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"
-
Every Espresso test class usually targets an Activity, therefore the name should match the name of the targeted Activity followed by Test,.
SignInActivityTest