Wednesday, September 28, 2011

PMD - Android Rules

This is the twenty fourth installment of explanation of PMD rules covering some Android rules.

Android Rules

These rules deal with the Android SDK, mostly related to best practices. To get better results, make sure that the auxclasspath is defined for type resolution to work.

CallSuperFirst

Super should be called at the start of the method

Example

public class DummyActivity extends Activity {
    public void onCreate(Bundle bundle) {
     // missing call to super.onCreate(bundle)
     foo();
    }
   }

CallSuperLast

Super should be called at the end of the method

Example

public class DummyActivity extends Activity {
    public void onPause() {
     foo();
     // missing call to super.onPause()
    }
   }

ProtectLogD

Log.d calls should be protected by checking Config.LOGD first

Example

public class DummyActivity extends Activity {
    public void foo() {
     Log.d("TAG", "msg1"); // Bad
 
     bar();
 
     if (Config.LOGD) Log.d("TAG", "msg1"); // Good
    }
   }

ProtectLogV

Log.v calls should be protected by checking Config.LOGV first

Example

public class DummyActivity extends Activity {
    public void foo() {
     Log.v("TAG", "msg1"); // Bad
 
     bar();
 
     if (Config.LOGV) Log.v("TAG", "msg1"); // Good
    }

No comments: