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.

Required fields*

9
  • Just yesterday I made a struct called "ValidationResult" and structured my code the way you describe. Commented Jan 24, 2013 at 13:14
  • 4
    It does not help to answer your question, but I just like to point out that you implicitly or purposely followed the Command-query separation principle (en.wikipedia.org/wiki/Command-query_separation). ;-) Commented Jan 24, 2013 at 13:21
  • Nice idea! One drawback: In your example, validation is actually performed twice: Once during Validate (returning False if invalid) and once during Save (throwing a specific, well-documented exception if invalid). Of course, the validation result could be cached inside the object, but that would add additional complexity, since the validation result would need to be invalidated on changes. Commented Oct 21, 2013 at 9:27
  • 1
    Another problem with this approach is that it can only be used if you are sure that there will be no interference from other threads or processes that might affect the validation result. By the time you call Save, the personData may no longer be valid. Commented Dec 16, 2014 at 10:36
  • 3
    This is better than the accepted answer i think. Throw when the call can't do the thing it was supposed to do. Commented Oct 29, 2015 at 1:32