Is defining get/set methods for every field overkill?
Fri Nov 18 09:42:21 UTC 2022
On Thursday, 17 November 2022 at 09:46:57 UTC, matheus wrote:
>
> Food for thought:
>
> https://yewtu.be/watch?v=_xLgr6Ng4qQ
>
> or
>
> https://www.youtube.com/embed/_xLgr6Ng4qQ
>
> Matheus.
'Food for thought'? Sure, if you're feeding that to your dog.
Public fields in 'class' definitions rarely have place in
production level code.
How would you enforce the business rules??
Lets say, you have declared a public member variable that is of
type int, but must be constrained to be within a particular range
of that int?
Well, you cannot enforce a business rule like that using a public
member variable. The user of your class can easily corrupt you
data, because they can access it directly.
Well, you enforce a business rule, by making it a private member
variable and having a public setter that accepts a value and
checks whether it meets the business rule, and only if it does,
and only if it does, will that value then be assigned to the
member variable.
That video link you presented is just dog food. Let your dog
think about it. In the meantime, you think about production level
code please.
More information about the Digitalmars-d-learn
mailing list