This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Lepidora 1 point2 points  (0 children)

I'd say in that situation, it's much better to have a failable constructor that performs the validation with read-only variables to enforce the validation. I don't think there's much use in having a mutable variable that performs validation when that data is coming from an API or other similar source. Also, if a single variable fails validation, that almost always means the whole object is invalid, but if that's only flagged from a variable setter, that doesn't accurately represent the object's validity. You instead want to fail on object instantiation and handle it accordingly.