Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

2
  • 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? Commented Jun 12, 2020 at 6:10
  • 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. Commented Jun 12, 2020 at 6:14