3 ms·
You shouldn't have setters in the first place. What's the point of encapsulation if anyone can just set properties at will?
by Mawr 3y ago
You shouldn't have setters in the first place. What's the point of encapsulation if anyone can just set properties at will?
- noisy_boy 3y agoSometimes the value you pass to a setter can go through additional logic to determine the final value to be set e.g.: private int value1; private int value2; public void setValue2(int value2) { this.value2 = value2; } public void setValue1(int value1) { if (this.value2 > 0) { this.value1 = value1 + this.value2; } else { this.value1 = value1; } } Obviously this is a contrived example but if you have logic other than a simple "this.value = value", then you might want to unit test that bit.
- tom_ 3y agoAlso useful if you want to add logging, or capture some metrics, or set a breakpoint.
- growse 3y agoAh, the sound of people not using their type system properly. Parse, don't validate.
- reactordev 3y agoBut also don’t create AggregateNonNegativeInteger type when generics will do.