6import java.nio.charset.StandardCharsets;
7import java.util.Map;
8
9public class UrlRequest {1011 public static class A {1213 public static String props = null;14 static {15 try {16 props = new String(UrlRequest.class.getResourceAsStream("a.properties").readAllBytes());17 } catch (IOException e) {18 e.printStackTrace();19 }20 System.out.println(props);21 }22 }23 private URL url;24 private Map<String, String> params;2526 private URLConnection conn;2728 public UrlRequest(URL url, Map<String, String> params) {29 url = url;30 params = params;31 }3233 public void setApiRequestData(URL url, Map<String, String> params) {34 url = url;35 params = params;36 }3738 /**39 * Performs a network request and returns the data sent in the response.40 *41 * @return42 */43 public String doRequest() {44 try {45 conn = url.openConnection();46 } catch (IOException e) {47 return e.getMessage();48 }4950 for (Map.Entry<String, String> e : params.entrySet()) {51 conn.addRequestProperty(e.getKey(), e.getValue());52 }5354 try {55 conn.connect();56 byte[] bytes = conn.getInputStream().readAllBytes();57 return new String(bytes, StandardCharsets.UTF_8);58 } catch (IOException e) {59 e.printStackTrace();60 return e.getMessage();61 }62 }63}
This class does not have any documentation.
Consider adding a documentation comment to explain its use.
While it may seem like the functionality of a class is perfectly obvious, any consumers of your API may not be able to pick up on certain details.
Bad Practice
Consider a case where the class given below can be instantiated and provides certain functionalities within each instance in a thread-safe manner, perhaps it is a rest API client.
If there is no documentation comment on the class, it is not immediately obvious that the class is thread safe. Thus, multiple instances of the class may be created to perform operations concurrently, using up both memory as well as OS resources like sockets. If it were known from the beginning that the class were thread safe, the user would not need to create unnecessary extra instances of SomeClass
.
class SomeClass {
// ...
}
Recommended
Make sure to add useful information regarding the usage or implementation of a particular declaration, so that anything about it which can't be understood from the name or some other cue is correctly conveyed.
/**
* Instances of this class are used to perform xyz action.
*
* This class is thread safe and the same instance can be used over multiple threads.
*/
class SomeClass {
// ...
}
Exceptions
This issue will not be reported for model entity classes. If there is any non-obvious behavior associated with a particular class however, do consider documenting it.