Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12111

Java DTO class with lots of fields being null

$
0
0

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 patters 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.


Viewing all articles
Browse latest Browse all 12111

Trending Articles