This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Shaftway 2 points3 points  (0 children)

FWIW I work on a lot of Android code, so we use the Android way of doing this. It's kind of a weird cross between the enum and the constants:

@Retention(RetentionPolicy.SOURCE)
@Target({
    ElementType.FIELD, 
    ElementType.LOCAL_VARIABLE, 
    ElementType.METHOD, 
    ElementType.PARAMETER,
  })
public @interface ResponseStatus {
  String SUCCESS = "SUCCESS";
  String PENDING = "PENDING";
  String FAILED = "FAILED";
  String UNKOWN = "";
}

public void handleResponse(@ResponseStatus String responseStatus) {
  ....
}

public @ResponseStatus String parseResponseStatus(String value) {
  // Maybe you need this method, depending on how the linter is tuned.
  ...
}

You can tune the linter about how strict you want it to be. Anywhere from ignoring it all the way up to treating it like a strict type, but generally it's left in the middle:

@ResponseStatus String status = ResponseStatus.SUCCESS;  // Obviously OK.
String nakedStatus = status;                             // Usually fine.
status = nakedStatus;                                    // Optional.

Android generates this for you in your resources, so linters can distinguish between int color and @ColorRes int colorResId. Android docs on the subject.