3 ms·
And the other alternative, because an object still usually needs some type of values would be: class Foo { Foo(string bar) { _bar = bar; } //Foo is the con
by scarface74 10y ago
And the other alternative, because an object still usually needs some type of values would be:
class Foo
{
Foo(string bar) { _bar = bar; } //Foo is the constructor
}
The benefit being that once an object is created, it is guaranteed to be in a valid state.
But by classical OO an object represents state + behavior. So why not do both?
Assign the value in the constructor to guarantee that the object is always in a valid state and use a property (if the object is an entity as opposed to a value object), when it needs to change?
- lacampbell 10y agoWhy is it changing? have the method name reflect that domain reason, not reflecting the mechanical details of which internal fields need to change.