patternjavaMinor
Wrapper library for Google Static Maps API
Viewed 0 times
googlemapswrapperforlibraryapistatic
Problem
I've recently read the books Clean Code and Effective Java.
I am a huge fan of Object-Oriented Java.
I am trying to write as professionally as possible. I have been an Android Developer for 20 months now.
Here is a class that I wrote that is supposed to communicate with Google Static Maps API. Is this code Clean, Effective and SOLID?
Also, there are a few nested classes here, should I create a package out of this or contain it in a single file as it is right now?
```
import static com.example.constants.GoogleStaticMapsAPIKeys.*;
public final class ParametrizedStaticMapsRequest {
private static final int DEFAULT_ZOOM_LEVEL = 15;
private static final int DEFAULT_WIDTH = 2 * SizeInPixels.MIN_WIDTH;
private static final int DEFAULT_HEIGHT = SizeInPixels.MIN_WIDTH;
private LatLngPoint centerValue = new LatLngPoint(0, 0);
private GoogleMapsImageFormat imageFormat = GoogleMapsImageFormat.JPG;
private GoogleMapsZoomLevel zoomLevel = new GoogleMapsZoomLevel(DEFAULT_ZOOM_LEVEL);
private SizeInPixels size = new SizeInPixels(DEFAULT_WIDTH, DEFAULT_HEIGHT);
private ParametrizedStaticMapsRequest() {
throw new AssertionError();
}
private ParametrizedStaticMapsRequest(Builder builder) {
centerValue = builder.centerValue;
imageFormat = builder.imageFormat;
zoomLevel = builder.zoomLevel;
size = builder.size;
}
@Override
public String toString() {
final String AMPERSAND = "&";
final String EQUALS = "=";
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(API_ROOT);
stringBuilder.append(AMPERSAND);
stringBuilder.append(PARAMETER_API_KEY);
stringBuilder.append(EQUALS);
stringBuilder.append(GOOGLE_STATIC_MAPS_API_KEY);
stringBuilder.append(AMPERSAND);
stringBuilder.append(PARAMETER_CENTER);
stringBuilder.append(EQUALS);
stringBuilder.append(this.centerValue.toString());
I am a huge fan of Object-Oriented Java.
I am trying to write as professionally as possible. I have been an Android Developer for 20 months now.
Here is a class that I wrote that is supposed to communicate with Google Static Maps API. Is this code Clean, Effective and SOLID?
Also, there are a few nested classes here, should I create a package out of this or contain it in a single file as it is right now?
```
import static com.example.constants.GoogleStaticMapsAPIKeys.*;
public final class ParametrizedStaticMapsRequest {
private static final int DEFAULT_ZOOM_LEVEL = 15;
private static final int DEFAULT_WIDTH = 2 * SizeInPixels.MIN_WIDTH;
private static final int DEFAULT_HEIGHT = SizeInPixels.MIN_WIDTH;
private LatLngPoint centerValue = new LatLngPoint(0, 0);
private GoogleMapsImageFormat imageFormat = GoogleMapsImageFormat.JPG;
private GoogleMapsZoomLevel zoomLevel = new GoogleMapsZoomLevel(DEFAULT_ZOOM_LEVEL);
private SizeInPixels size = new SizeInPixels(DEFAULT_WIDTH, DEFAULT_HEIGHT);
private ParametrizedStaticMapsRequest() {
throw new AssertionError();
}
private ParametrizedStaticMapsRequest(Builder builder) {
centerValue = builder.centerValue;
imageFormat = builder.imageFormat;
zoomLevel = builder.zoomLevel;
size = builder.size;
}
@Override
public String toString() {
final String AMPERSAND = "&";
final String EQUALS = "=";
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(API_ROOT);
stringBuilder.append(AMPERSAND);
stringBuilder.append(PARAMETER_API_KEY);
stringBuilder.append(EQUALS);
stringBuilder.append(GOOGLE_STATIC_MAPS_API_KEY);
stringBuilder.append(AMPERSAND);
stringBuilder.append(PARAMETER_CENTER);
stringBuilder.append(EQUALS);
stringBuilder.append(this.centerValue.toString());
Solution
I'm O.K. with the inner classes, but I tend to use them more than others. I would say that you are "reaching the limit" and if there were many more you would want to split them off.
Consider adding a utility method to do all the repeated &key=value stuff, e.g. something like:
In this case, naming the constants AMPERSAND and EQUALS is fine, and probably better than trying to come up with a "what they do" name.
Also, instead of using
Which would be referred to in the code as
Addendum Suggestion
Make the fields of ParametrizedStaticMapsRequest final. The fields of the Builder should be initialized to "good defaults", like JPG and DEFAULT_ZOOM_LEVEL, then modified by the user.
If it makes sense, you could then define some statics like
Consider adding a utility method to do all the repeated &key=value stuff, e.g. something like:
public static StringBuilder addKV(StringBuilder stringBuilder, String key, String value) {
stringBuilder.append(AMPERSAND);
stringBuilder.append(key);
stringBuilder.append(EQUALS);
stringBuilder.append(value);
return stringBuilder;
}In this case, naming the constants AMPERSAND and EQUALS is fine, and probably better than trying to come up with a "what they do" name.
Also, instead of using
static import GoogleStaticMapsAPIKeys to save typing, but then adding a "PARAMETER_" to the start of all those key names for clarity, consider giving that class a shorter name, not statically importing, but lose the "PARAMETER_" stuff. e.g.public class MapsAPIKey {
public static final String CENTER = "center";
... etc...
}Which would be referred to in the code as
MapsAPIKey.CENTER etc... To my eye that is more natural, YMMV.Addendum Suggestion
Make the fields of ParametrizedStaticMapsRequest final. The fields of the Builder should be initialized to "good defaults", like JPG and DEFAULT_ZOOM_LEVEL, then modified by the user.
If it makes sense, you could then define some statics like
public static final ParametrizedStaticMapsRequest DEFAULT_JPG = new ParametrizedStaticMapsRequest (new Builder());Code Snippets
public static StringBuilder addKV(StringBuilder stringBuilder, String key, String value) {
stringBuilder.append(AMPERSAND);
stringBuilder.append(key);
stringBuilder.append(EQUALS);
stringBuilder.append(value);
return stringBuilder;
}public class MapsAPIKey {
public static final String CENTER = "center";
... etc...
}Context
StackExchange Code Review Q#86590, answer score: 2
Revisions (0)
No revisions yet.