Probably asked before, but could not find it.
I am writing a lot of statements in the form:
if (bar.getFoo() != null) {
this.foo = bar.getFoo();
}
I thought of the ternary operator, but that doesn't make it much better in my opinion:
this.foo = bar.getFoo() != null ? bar.getFoo() : this.foo;
My main problem is that I have to write bar.getFoo() twice in both situations. Of course I can write a helper method that solves this, but I was wondering if there were more elegant solutions.
Different from: Avoiding != null statements because that question does not deal with assigning to a value the value that is null checked on.