With my team we are refactoring an app and we're adding lombok to help writing (and reading) pojo class.
There are differents issues with this refactoring and some of you may have hives after reading it, but mistakes make us grow !!
Here is the class before,
package eu.middle.services.app.generiques.model;
import java.time.LocalDate;
import java.util.List;
public class Stabilite {
private LocalDate dateMiseAJour;
private LocalDate dateFin;
private List<Molecule> liste;
public Stabilite() {
super();
}
public Stabilite(LocalDate dateMiseAJour, LocalDate dateFin, List<Molecule> liste) {
super();
this.dateFin = dateFin;
this.dateMiseAJour = dateMiseAJour;
this.liste = liste;
}
public LocalDate getDateFin {
return dateFin;
}
public void setDateFin(LocalDate dateFin) {
this.dateFin = dateFin;
}
@Override
public boolean equals(Object obj) {// NOSONAR
....
}
// and the other getter/setter, hasCode, toString
}
and here is the class after, much more easier to read with lombok.
package eu.middle.services.app.generiques.model;
import java.io.Serializable;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Stabilite implements Serializable {
private static final long serialVersionUID = 2356231377315682966L;
private LocalDate dateMiseAJour;
private LocalDate dateFinObservation;
private List<Molecule> liste = new ArrayList<>();
}
He transform the pojo class in a bean (interface implementation and UUID adding), it's not our problem here as we often say all the Bean files can be POJOs, but not all the POJOs are beans, (maybe I'm wrong in my case), as the unuseful @NoArgsConstructor(this one I'm sure)
My main problem is private List<Molecule> liste = new ArrayList<>();, I'm not ok with this, because here you see one class but almost a hundred class have been modified this way
Questions
- Won't it use memory unnecessarily ?
- Won't this affect the polymorphism ?
- What are the others issues I don't think of ?
Thanks in advance
Won't it use memory unnecessarily ?
It will use a bit, the question is if you expect this class to be initialized with list anyway. If yes than there will be no difference. If no it is better to make it lazy initialized.
Won't this affect the polymorphism ?
It will not as this class is mutable and you can change it in a setter later to LinkedList eg.
What are the others issues I don't think of ?
Not much else TBH, if you really care about the space and want to have lazy initialization you can use:
This would initialize the class only when the getter is called.