1. Introduction

Refactoring old code comes across a lot of things like this:

    public void attend(String value) {
        if ("0".equals(value)) {
            //todo 
        } else if ("1".equals(value)) {
            //todo
        } else {
            //todo }}Copy the code

Head ache! Java is syntactically sound, but business is confusing to understand the meanings of 0 and 1, which are collectively known as magic values. For the above code we often need to infer logic from the context, especially if it is very complex business or code from 10 years ago, or even if there is no documentation. For readability, we try to avoid mana. Today we’ll talk about a few ways to avoid mana.

2. Avoid some operations on mana.

In general, the mana value does not change very often. The handling of mana values is combined with business and scope.

2.1 Static Constants

If the value is scoped in a class or in the same package, static constants can be used.

    private static final String FEMALE = "0";
    private static final String MALE = "1";
    public void attend(String value) {
        if (FEMALE.equals(value)) {
            //todo
        } else if (MALE.equals(value)) {
            //todo
        } else {
            //todo}}Copy the code

This makes it a lot clearer that 0 and 1 are gender (with your good naming habits).

2.2 Using Interfaces

Since we are using static constants, we can seal the magic value into the interface.

public interface Gender {
    String FEMALE = "0";
    String MALE = "1";
}
Copy the code

2.3 Using Enumeration

But the point of an interface is to provide abstract functionality rather than store constant values, which clearly defeats the purpose of interface design. So JDK1.5 introduces the enum type enum.

public enum GenderEnum {
    FEMALE,
    MALE
}
Copy the code

Genderenm.ordinal () and/or the string name of the enumeration from genderenm.male-.ordinal (). They can be used for some logical identification in most cases. However, not satisfying our original design, we need to modify the constructor of the enumerated class.

public enum GenderEnum {

    FEMALE("0"),
    MALE("1");

    private final String value;

    GenderEnum(String value) {
        this.value = value;

    }

    public String value(a) {
        return this.value; }}Copy the code

So now that we can rewrite this, we can use the value() method to get the actual value.

Let’s add a few more requirements for ourselves to make your enumeration more readable.

public enum GenderEnum {

    UNKNOWN("1"."Unknown"),
    FEMALE("0"."Women"),
    MALE("1"."Men");

    private final String value;
    private final String description;

    GenderEnum(String value, String description) {
        this.value = value;
        this.description = description;
    }

    public String value(a) {
        return this.value;
    }

    public String description(a) {
        return this.description; }}Copy the code

The description value not only helps us know what the enumeration actually represents, but it can even be returned to the front-end business as an illustration.

Tip: Try not to use Chinese declarations for enumerations, such as FEMALE directly declared as FEMALE. In addition, enumerations are singletons, so you can’t use clone and deserialization.

3. Summary

Today we learned how to elegantly handle magic values in coding, especially enumeration schemes. I hope it works for you.

Follow our public id: Felordcn for more information

Personal blog: https://felord.cn