Skip to main content
The 2024 Developer Survey results are live! See the results

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

6
  • 2
    But if I do this, I'm gonna have to edit the method each time I add a field to the class. Besides, a generic method can be put in an abstract class and extended to be used in all other Value Objects
    – th3an0maly
    Commented Sep 12, 2012 at 10:08
  • 4
    Yes, you will. I'm not sure why you think that's a problem. You know what fields you're adding, you have to create setters and getters for them. Adding a simple one extra line of checking is not a lot of extra work.
    – paxdiablo
    Commented Sep 12, 2012 at 10:23
  • 2
    @Birger, if that's what they meant, they should have said that. They've had ample opportunity to change it in the intervening four-or-so years :-) In any case, as others have also suggested, this is a perfectly acceptable method unless your class is so unstable, it's having member variables added or removed massively frequently.
    – paxdiablo
    Commented Sep 14, 2016 at 12:20
  • 1
    I concurr, the classes should not be changed. OP did however write that he'd rather not have to check (variable == null) for each variable, and I sympathize with that. It would be neat to have some general way of checking if fields in any given object are null (or not)
    – birgersp
    Commented Sep 14, 2016 at 15:18
  • 1
    @paxdiablo just as an aside, one argument for a more dynamic approach is when you're converting, say, a DTO to a JPA entity-style object or something. If you don't have a way of dynamically checking for any null properties, it gets harder to write a unit test that will fail when you forget to add a conversion for that new property you just added to the DTO.
    – ebwb
    Commented Apr 16, 2019 at 20:23