# Writing a class for common block of code used in many other classes

In this I have written the setAnalyticsInfo() method. The code inside this is common to almost all other (30) classes, so I have created GoogleAnalyticsCode and defined one common method and calling this method from all other classes.

import com.google.analytics.tracking.android.EasyTracker;

import android.app.Activity;

private String caption;
private Activity activity;
public GoogleAnalyticsCode(String caption, Activity activity) {
super();
this.caption = caption;
this.activity = activity;
}

public void setAnalyticsInfo(){
EasyTracker easyTracker = EasyTracker.getInstance(activity);

if(easyTracker!=null)
{
easyTracker.send(MapBuilder
.createEvent(caption,     // Event category (required)
caption,  // Event action (required)
caption,   // Event label
null)            // Event value
.build());
}
}
}


I am calling setAnalyticsInfo() method like this in other classes in NavDrawer.java:

      public class NavDrawer {
public void navigate_to_rating() {
gac.setAnalyticsInfo();

final String appPackageName =activity.getPackageName(); // getPackageName() from Context or Activity object
try {
activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
} catch (android.content.ActivityNotFoundException anfe) {
}
}
}


My question is:

Is it good programming standard to write separate classes for common blocks of code used in many other classes?

• check it is working code. I have changed example now to make it working because the previous example was taken from my android apps code but it needs some jars to make it workable at your end so just for sake of working code i have given very simple logic. Oct 8 '14 at 5:36
• "write separate classes for common blocks of code used in many other classes"? Depends. In the example code that you have just showed, no, I wouldn't write a new class just to do addition. If you are comfortable sharing real code from your Android application, we may be able to offer a more thorough advice. Cheers! Oct 8 '14 at 6:13
• Thanks for your positive feedback. I will share my apps code. Oct 8 '14 at 8:14

Is it good programming standard to write separate classes for common blocks of code used in many other classes?

Yes, it is. Not repeating code over and over again is an important principle of programming.

The alternative to creating a separate class would be to create a Base activity which contains this method (in cases that you feel a separate class would be overkill). You could even call this method in the onCreate method of the Base activity, thus automatically using it each time.

In this case it's really a matter of taste. I would probably put it in a method in the Base activity, because your current GoogleAnalyticsCode class doesn't really contain any logic. It just calls EasyTracker:send().

Coding Style

Just a quick side-point concerning your coding style: You should try to be consistent with the position of your opening curly bracket, your indentation, and your spacing.

• Thanks tim. Though the GoogleAnalyticsCode class doesn't contain any big logic but each time (say 5 methods per class) and there are approximately 30 classes where I am using this common code.That's why I made it a separate class. Anyway some of my classes are already extended by Activity (a built in class of Android) so I cannot again extend by other classes(like BaseActivity). So now tell me whether this is a good coding practice or not? Oct 8 '14 at 12:57
• BaseActivity would extend Activity. And then all your other activities would extend BaseActivity. I think it's always good to have a base activity, because you have a place for common code, and to define behavior that is the same across all activities. And as I said, not repeating code is very important. How you do it is a matter of opinion, but your approach certainly isn't bad.
– tim
Oct 8 '14 at 13:01

It is fine, however, while I dont know if you have much more code in those classes going on, you could even declare setAnalyticsInfo() static with the class constructor params and write a Utility class with private Constructor and static methosd only. Utility class are fine. Another way is to make GoogleAnalyticsCode to a Singleton. There are different ways to acomplish it, google is your friend here, but be aware of synchronization! And finally you could do DI (Depedency Injection) e.g. with Guice, or just with common JEE. Creating GoogleAnalyticsCode again and again everywhere you want to use it seems to be inappropriate, without knowing the context.