-1

Looking and digging through my code, I noticed that we usually use the @Setter from Lombok thoroughly. It made me wonder about the usage of @Setter.

Indeed, is it more interesting to use @Setter than simply making the field accessible through its visibility?

From what I understand, setters are preferable in one of those cases (thanks to Why use getters and setters/accessors?):

  • you want your object to react to a field being set
  • you want to lazily compute the value of a field
  • you make want to mock (although I deeply think testability should not lead your code design)
  • class may be inherited and setters overridden

But there are some object which does not match any of those cases.

In code terms, is this:

@Getter
@Setter
public class Person{

  private String firstName;
  private String lastName;

}

more preferable (in terms of maintainability and readability) than this:

public class Person{

  public String firstName;
  public String lastName;

}

As a side note, I would like to add that I find person.firstName = "Vincent" more readable than person.setFirstName("Vincent").

Vincent C.
  • 607
  • 5
  • 18
  • "although I deeply think testability should not lead your code design". Wow. – Kayaman Oct 31 '19 at 10:34
  • you are 1 step away from e.g. Groovy – injecteer Oct 31 '19 at 10:36
  • @Kayaman what are you thoughts on this? Could you elaborate? I am an adept of 100% coverage through mutation coverage. But modifying your code to simply make it testable is dull, don't you think? If you have any article/resource about this topic, I would happily read it! – Vincent C. Oct 31 '19 at 10:41
  • @VincentC. I don't think you have enough experience to properly consider this. You're asking about basic encapsulation, and saying how you don't feel writing testable code is a good idea. I can't think of too many people who would agree with you, so I hope that with further experience you might understand the importance of testability. – Kayaman Oct 31 '19 at 10:46
  • Somehow. I just feel that it does not answer whether there are some cases where a setter might not be of use. – Vincent C. Oct 31 '19 at 10:46
  • 2
    Your question seems to fundamentally relate to using setters vs not using setters, *not* using lombok vs not using lombok. – Michael Oct 31 '19 at 10:47
  • @Kayaman thanks for the answer, would you mind sharing resource or anything that would argue about your code design being driven by testability? I really would love to see arguments on this to change my point of view. – Vincent C. Oct 31 '19 at 10:50
  • 1
    @VincentC. the internet is full of resources that promote testable code, with [TDD](https://en.wikipedia.org/wiki/Test-driven_development) being at the extreme end. It's basically a question of "do you want high quality code", and your opinion is "it's not *that* important". Especially if you can't give arguments why we shouldn't write testable code, but I can't imagine any other reasons besides "let's do this quickly, get the money and run". – Kayaman Oct 31 '19 at 11:10
  • @Kayaman you're definitely right. I am myself using TDD for every of my projects. But to me, writing something not unitary testable is sign of bad design, but the opposite is not always true. This is what I meant saying that I don't want my code to be driven by testability, I would prefer just using correct patterns, whether or not it would be hard to test. Sorry for the misunderstanding :) – Vincent C. Oct 31 '19 at 11:39

1 Answers1

1

You are completely mixing encapsulation with visibility.

Firstly, it's not about lombok. Lombok avoid bolier plate code, in this case, having a setter method. Java beans or DTOs have getter and setter method usually by convention and lombok does it at generated class rather than the java file.

Karthik R
  • 5,523
  • 2
  • 18
  • 30
  • It is about Lombok in the point that Lombok does not allow (by design) to write anything in your setter, hence the points about lazy initialization or reaction to setting a value makes no sense for such a case. – Vincent C. Oct 31 '19 at 10:43
  • 2
    If you want to write something in your setter don't use lombok. Simple as that. It's just a helper library to avoid boiler plate code. – Karthik R Oct 31 '19 at 10:44