Timeline for Where to validate user input - Constructor, Validation object, or Factory method?
Current License: CC BY-SA 4.0
3 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Jun 12, 2020 at 6:14 | comment | added | candied_orange | If the object is immutable holding "for the life of" is not an issue. What I'm distinguishing between is where the need for the restriction comes from. People use signed integers for positive only values all the time by controlling what they can be set to. This is the same trick. If external use drives the restriction, not internal behavior, then validating in the factory associated with that use makes a lot of sense. | |
| Jun 12, 2020 at 6:10 | comment | added | anon | I'm not sure I follow. I looked it up and "invariant" is something that must hold true for the life of an object (except as its being modified). That's what I'm trying to do, but I think where I'm having issues is how "close to the invariant" is sufficient? If the class cannot ever be invalid, then the constructor would need to throw the error. At least in the language of my project (PHP) it doesn't seem easy to send a list of errors as part of the exception payload, hence the question about appropriately DRYing my validation checks. Did I misunderstand your answer? | |
| Jun 12, 2020 at 5:33 | history | answered | candied_orange | CC BY-SA 4.0 |