This is a general coding style question regarding which style is more general or elegant.
When reading Java collection source code, I found the first style more frequently than the second one. Could anyone shred some light/ reasoning on this? IMHO, the second style is more concise than the first one, but actually, the first one should be redeemed more elegant in some way I cannot argue.
- creating local variable.
private Set<K> keySet;
public Set<K> keySet() {
Set<K> ks = keySet;
if (ks == null) {
ks = new KeySet();
keySet = ks;
}
return ks;
}
- operating on class member variable.
public Set<K> keySet() {
if (keySet == null) {
keySet = new KeySet();
}
return keySet;
}