-1

I am currently working with a Java DTO class (written by others) that has almost 300 fields where many of these end up being null. What I see in the codebase are lots of null checks and more recently I have been using Optional.ofNullable to work around this (given I come from a functional background) and find that easier to work with.

However, I would like to avoid having to remember to do that everywhere I need to access one of those fields and possibly return a different kind of object where the return type is always of Optional<T>.

There is a similar post here about the problem in Python but I would be interested in what can also be done in Java. I can of course just live with it or generate a separate class with optional getters myself but was interested if anyone has any smarter approaches or good design patterns to use for such cases.

In my case when a field is null the most common scenario is not to throw a exception but to use some fallback mechanism or default value which is why the Optional class works nicely.

4
  • 4
    What is your actual problem? What's wrong with null checking? If you want convenience, have you tried making your fields Optional<your-field-type>? Jackson supports Optional fields with nothing special extra btw.
    – Bohemian
    Commented Apr 17 at 10:44
  • Any suggestion to a solution would be heavily influenced by what you want to do if a field is null. Do you throw an exception, choose a default value or skip the process asking for the field?
    – ODDminus1
    Commented Apr 17 at 10:48
  • I feel the type should convey what needs to be checked and having to check for null every time feels silly and that I am repeating myself. If you look at the link I posted about the equivalent problem posed in Python I am looking for a Java solution of a similar form. I can indeed change the fields to be Optional but it makes it a big refactoring exercise that I prefer to avoid and would like to make the change more gradual or at least give developers the choice as to how to view the DTO.
    – Leon
    Commented Apr 17 at 10:50
  • The most common use case when the field is null is not to throw an exception but to use some fallback mechanism or default value which is why Optional works very nicely in my use case.
    – Leon
    Commented Apr 17 at 10:51

1 Answer 1

0

I finally had some time to come back to this and what I ended doing in the end was to use some basic templating to generate a new class from the original one, returning Optional<T> get methods as shown below.

  public class WithOptionalGettersClass {
    private final class WithNullableGettersClass nullFields;

    public WithOptionalGettersClass(WithNullableGettersClass nullFields) {
      this.nullFields = nullFields;
    }

    public Optional<String> getName() {
      return Optional.ofNullable(this.nullFields.getName());
    }

    public Optional<String> getComment() {
      return Optional.ofNullable(this.nullFields.getComment());
    }

    public Optional<LocalDate> getDate() {
      return Optional.ofNullable(this.nullFields.getDate());
    }
    
    // With many more such getOptionalXXX methods below

If one wants to take this further then this "auto-generation" of the new class with optional getters could be integrated into the relevant build pipeline.

I hope others find this useful.

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