if(obj.getAttribute() != null) {
newobject.setAttribute(obj.getAttribute());
}
if(obj.getAttribute() != null) {
newobject.setAttribute(obj.getAttribute());
}
if(obj.getAttribute() != null) {
newobject.setAttribute(obj.getAttribute());
}
Imagine this code at scale - mapping an object with 15+ attributes. How do I reduce cognitive complexity without splitting the mapping in two/three parts which increases the cognitive complexity overall.
You might use Optional
like
newobject.setAttribute(
Optional.ofNullable(obj.getAttribute()).orElse("your_default_value")
);
But it depends of what should happen when obj.getAttribute()
is empty ?
From what is provided newobject.attribute
will be empty anyway, assigning property from parent does not require null-check.