Reputation: 6163
I found the following snippet in our code base. This is code that is widely used and hasn't caused any issues, and I can't figure out why.
element = someBoolean ?
element = parentElement :
element;
Shouldn't the second line be resetting element
to an empty value or something after it is set to parentElement
ETA: I am wondering what the return value of the element = parentElement
clause would be because it didn't seem like there was any return value over there. I forgot that there is a concept of multiple assignments in java, so element would just be set twice.
Upvotes: 1
Views: 56
Reputation: 10652
The assignment is quite useless, since your code can be written as...
element = someBoolean ?
parentElement:
element;
Only in your code, you do not return parentElement
immediately, but first set element
to parentElement
and then element
to element
.
Upvotes: 3
Reputation: 8376
Shouldn't the second line be resetting element to an empty value or something after it is set to parentElement
I don't know why you would think that. The purpose of the entire statement is to potentially reassign element
to parentElement
. The statement is equivalent to:
element = someBoolean ?
parentElement:
element;
This is because an assignment statement (element = parentElement
) has a value of the RHS (i.e. parentElement
). I would simply write it this way, though:
if (someBoolean) {
element = parentElement;
}
Upvotes: 8