14 Replies
basically, method vs setter
but that's not the main difference between these
Those are two different concepts; the first one you're capable of setting however you want, even negative numbers. The second is more closely controlled and tied to a behavior; grow always increments by one.
They're not even mutually exclusive
so if my grow() method is ONLY concerned with incrementing the age, better to just use the setter?
and if i had more things like aging, gaining height, etc, i use a method instead?
It depends, that's not really what I said; in pointed out they do two different things
Why isn't age++ considered tied to a behavior
It seems semantically meaningful enough compared to grow()
he means that making it a publicly settable property isn't "tied to a behavior"
cause making it a settable property allows one to do a LOT more with it than just
++
^
by making the age setter protected and putting the operation inside the
grow()
method, that allows the class to control exactly what can happen to the age
property
the difference is between saying "you can increment age" and "you can set age to anything you want" to code outside of the classyou are comparing a data class (which could be anything: an entity, an event, a message) with a business logic class
indeed
second one follows "tell, don't ask". it hides implementation details (like having the value in a property) behind a method interface and thus limits the complexity of the interface which in the end increases maintainability as you can change the internal implementation without breaking the interface.
Good Thing! Second one is the winner of clean-code enthusiasts 😄
okay thank you
It's not really a clean code issue
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.