Thursday, 27 September 2018

Mutable members should not be stored or returned directly

public Date getstartDate()
{
return startDate;
}
public void setstartDate(Date startDate)
{
this.startDate = startDate;
}
view raw StartDate1.java hosted with ❤ by GitHub

But SonarLint is complaining about it, so I refactored it into this:

public Date getstartDate()
{
return startDate == null ? null : new Date(startDate.getTime());
}
public void setstartDate(Date startDate)
{
if (startDate != null) {
this.startDate = new Date(startDate.getTime());
} else
{
this.startDate = null;
}
}
view raw StartDate2.java hosted with ❤ by GitHub

It's high time this old codebase gets converted to LocalDates.

References

SonarSource - RSPEC-2384
https://rules.sonarsource.com/java/RSPEC-2384

No comments:

Post a Comment