patternjavaModerate
Refactoring long class to make it more maintainable
Viewed 0 times
refactoringmorelongmakemaintainableclass
Problem
I am developing an Android app. Due to my low experience and the requirement for network operation, my class uses a lot of
I am really confused in cases where I have to deal with UI elemnts.
I wish to see some small code snippets which will demonstrate good practice. I am not asking for a full and really detailed answer, as it probably will be very long, but it would be really nice.
(Should I add some comments?)
```
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import org.apache.http.annotation.ThreadSafe;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import ua.mirkvartir.android.frontend.adapter.GPSTracker;
import ua.mirkvartir.android.frontend.adapter.JSONParser;
import ua.mirkvartir.android.frontend.adapter.UserFunctions;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.content.CursorLoader;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox
AsyncTask instances and it grew quite large. I'd like to know how I can split this in to different classes, and generally make it better (not required).I am really confused in cases where I have to deal with UI elemnts.
I wish to see some small code snippets which will demonstrate good practice. I am not asking for a full and really detailed answer, as it probably will be very long, but it would be really nice.
(Should I add some comments?)
```
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import org.apache.http.annotation.ThreadSafe;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import ua.mirkvartir.android.frontend.adapter.GPSTracker;
import ua.mirkvartir.android.frontend.adapter.JSONParser;
import ua.mirkvartir.android.frontend.adapter.UserFunctions;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.content.CursorLoader;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox
Solution
A few tips, but there may be more. These are just the things that immediately jumped out at me:
Break out AsyncTasks into their own class
With multiple tasks, it might be better to make them each their own class/file. You can call back to the main Activity with listeners. If you must keep them in place, at least group them together at the end of the file. There's no reason to have them right in the middle of another class, with methods on both sides.
Combine the button listeners
Instead of making several anonymous listeners, you can let the Activity implement the listener and have all the corresponding code in one neat method. There, you switch on button id to call individual methods to handle each. This is a personal style issue, but I think it makes it much more readable to have one
Combine the
The only difference I see between
Learn to use
Big
Break out AsyncTasks into their own class
With multiple tasks, it might be better to make them each their own class/file. You can call back to the main Activity with listeners. If you must keep them in place, at least group them together at the end of the file. There's no reason to have them right in the middle of another class, with methods on both sides.
Combine the button listeners
Instead of making several anonymous listeners, you can let the Activity implement the listener and have all the corresponding code in one neat method. There, you switch on button id to call individual methods to handle each. This is a personal style issue, but I think it makes it much more readable to have one
onClick() function.Combine the
addPhoto() methodsThe only difference I see between
addPhoto1() and addPhoto2() is the request code. Combine them into one addPhoto(int reqCode) method. If you add more later, it will be much easier than copy/pasting a whole new method called addPhoto7(), etc.Learn to use
switch more, or refactor those if blocksBig
if/else blocks are ugly, and can sometimes be rewritten with a single switch. Sometimes, you need a bit more. For instance, in your onActivityResult() method, you can easily check resultCode just once, with a switch inside that. Same with the data == null checks.Context
StackExchange Code Review Q#30289, answer score: 12
Revisions (0)
No revisions yet.