Moving away from @Data annotation, a step at a time

Lombok is used extensively in the open-lmis. The @Data annotation can be seen on majority of classes. A closer look at the code suggests that the only reason that the @Data annotation is used is to generate getter and setters for all member variables. There are many disadvantages to the continued “@Data” annotation usage in the code base.

  • @Data exposes all private members when not everything needs to be public. This breaks object oriented programming principles.
  • @Data generates more than getters and setters. These methods are not necessarily what developers have thought through, and are untested.
  • The behavior of @Data generated methods when the class has inherited another class that has the annotation or when the class overrides equals or hashcode or canEqual* is not clear.
    In general, I think @Data has encouraged laziness. Yes, it may still have a place for code examples. But not in a mature, complex product. Considering where we are, I think refactoring in the following steps may get us to a more expressive, and less inappropriate usage of lombok in open-lmis.
Where we are (Lets please move away from this)

@Data

public class DosageUnit extends BaseModel {

  private String code;

  private int displayOrder;
}


Step 1: Remove @Data annotation and replace it with @Getter and @Setter. These are more specific. though this generates getter and setters for all methods, it does cut down all the other functions that @Data generates. I think this is an appropriate step in the right direction. It is also relatively easy to implement.

@Getter @Setter
public class DosageUnit extends BaseModel {

private String code;

private int displayOrder;

}

Step 2: Be more specific with which methods will be exposed with getter and setter. The annotation should be at the field level and not at the class. I think it would be nice if new classes started with such specificity.

public class DosageUnit extends BaseModel {

@Getter @Setter
private String code;

@Getter @Setter
private int displayOrder;

}

If we decide to move in this direction, Lombok project itself may have configuration setting to either raise a warning or error when @Data is being used. This configuration may be helpful.

lombok.data.flagUsage = [warning | error] (default: not set)
Lombok will flag any usage of @Data as a warning or error if configured.

The reference of *methods in step 1 and step 2 was an error. I meant to say member variables

···

On Tuesday, December 15, 2015 at 12:23:05 PM UTC-5, Elias Muluneh wrote:

Lombok is used extensively in the open-lmis. The @Data annotation can be seen on majority of classes. A closer look at the code suggests that the only reason that the @Data annotation is used is to generate getter and setters for all member variables. There are many disadvantages to the continued “@Data” annotation usage in the code base.

  • @Data exposes all private members when not everything needs to be public. This breaks object oriented programming principles.
  • @Data generates more than getters and setters. These methods are not necessarily what developers have thought through, and are untested.
  • The behavior of @Data generated methods when the class has inherited another class that has the annotation or when the class overrides equals or hashcode or canEqual* is not clear.
    In general, I think @Data has encouraged laziness. Yes, it may still have a place for code examples. But not in a mature, complex product.

Considering where we are, I think refactoring in the following steps may get us to a more expressive, and less inappropriate usage of lombok in open-lmis.

Where we are (Lets please move away from this)

@Data

public class DosageUnit extends BaseModel {

  private String code;

  private int displayOrder;
}




Step 1: Remove @Data annotation and replace it with @Getter and @Setter.  
These are more specific. though this generates getter and setters for all member variables, it does cut down all the other functions that @Data generates.  
I think this is an appropriate step in the right direction. It is also relatively easy to implement.


@Getter @Setter
public class DosageUnit extends BaseModel {

private String code;

private int displayOrder;

}

Step 2: Be more specific with which member variables will be exposed with getter and setter.

The annotation should be at the field level and not at the class.

I think it would be nice if new classes started with such specificity.

public class DosageUnit extends BaseModel {

@Getter @Setter
private String code;

@Getter @Setter
private int displayOrder;

}

If we decide to move in this direction, Lombok project itself may have configuration setting to either raise a warning or error when @Data is being used. This configuration may be helpful.

lombok.data.flagUsage = [warning | error] (default: not set)
Lombok will flag any usage of @Data as a warning or error if configured.