2
\$\begingroup\$

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 com.google.analytics.tracking.android.MapBuilder;

import android.app.Activity;

public class GoogleAnalyticsCode {
    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() {
        GoogleAnalyticsCode gac = new GoogleAnalyticsCode("Rate Us", activity);
        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) {
            activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=" + appPackageName)));
        }
    }
    }

My question is:

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

\$\endgroup\$
3
  • \$\begingroup\$ 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. \$\endgroup\$
    – Programmer
    Oct 8 '14 at 5:36
  • \$\begingroup\$ "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! \$\endgroup\$
    – h.j.k.
    Oct 8 '14 at 6:13
  • 1
    \$\begingroup\$ Thanks for your positive feedback. I will share my apps code. \$\endgroup\$
    – Programmer
    Oct 8 '14 at 8:14
1
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ 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? \$\endgroup\$
    – Programmer
    Oct 8 '14 at 12:57
  • \$\begingroup\$ 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. \$\endgroup\$
    – tim
    Oct 8 '14 at 13:01
0
\$\begingroup\$

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.

\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.