-1

So I was sending code for review and approvals for some changes I made recently. that includes a class as below:

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class UnKnownClientException extends CustomException {

public UnKnownClientException (String message, Throwable throwable) {
        super(message, throwable);
    }
}
@Setter
@Getter
public abstract class CustomException extends ContextedRuntimeException {

    private ApiException apiException;

    public CustomException (String message, Throwable throwable) {
        super(message, throwable);
    }

}

so there was a debate with my colleague as to use @Data vs @Getter, @Setter.

I was reasoning that as we are not using any other @Data shortcuts except, @Getter, @Setter, I added @Getter, @Setter but my colleague argues that it is safe to use @Data as a good practice. But I think as we have a custom constructor even with @Data we have to add that code manually. So not sure which side is correct. Looking for more insights..

1
  • 1
    The answers to this question as-is will likely be opinion-based, so try to rephrase it to elicit more objective answers. Btw, the snippet of UnKnownClientException that you've posted doesn't need any Lombok. Arguably, exceptions should be @Getter only to prevent their fields from changing as the stack unwinds. Commented Aug 11, 2022 at 20:39

1 Answer 1

3

@Data will turn your class into a full fledged DTO. Rather then simply letting you make your private members public it also implements toString, equals, and hash. Since you're not building a polymorphic encapsulated behavior object anyway you might as well add all the @Bells and @Whistles.

Yes it sticks you with a constructor you don't need, and thanks to those setters, lets anyone fiddle with it's data at any time but hey, you only live once. Some people insist on only adding what you really need. But they're no fun.

That might read as sarcasm but seriously it sounds like this codebase already doesn't care about ISP. Pick your fights wisely.

3
  • sorry if I am asking a dumb question but why do you think the code base does not follow ISP? Commented Aug 11, 2022 at 20:58
  • Your setter, your colleagues data, and that you're using lombok doesn't help. I haven't looked at your code base but I have my suspicions. Feel free to dissuade me. Commented Aug 11, 2022 at 21:06
  • 1
    Let me put it this way: Why are you making an exception into a DTO at all? Do you have a real reason or is this just enthusiastic use of lombok? Commented Aug 12, 2022 at 17:02

Not the answer you're looking for? Browse other questions tagged or ask your own question.