If I have a validator class with a validate function that takes a ruleContainer as input. Should the validation pass if the container is empty ( least astonishment?) or should I throw an exception or just return false ?

closed as unclear what you're asking by gnat, 8bittree, Thomas Owens Oct 10 '17 at 15:23

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.


Should the validation pass if the container is empty ( least astonishment?)


Answering the question at the level of abstraction you're posing, which is to say without assuming or imposing any additional domain requirements.

If we consider that the ruleContainer holds a set of rules that are constraints, and that its rules all taken together are used in conjunction (e.g. ANDed). Then, the absence of rules suggests the absence of constraints and hence the absence of violations. Thus, validation should succeed. This would be the logical approach. In other words, only if one or more rules is violated should validation fail.

This from Logical Conjunction

The conjunctive identity is 1, which is to say that AND-ing an expression with 1 will never change the value of the expression. In keeping with the concept of vacuous truth, when conjunction is defined as an operator or function of arbitrary arity, the empty conjunction (AND-ing over an empty set of operands) is often defined as having the result 1 [or true]. [my emphasis]

When we change the behavior of the empty set from the logical/expected behavior (e.g. either when giving or when returning an empty set) it means that calling client code will have to consider this special case, and, the intent of the translation may get lost by the caller. The special case handling (by the implementation or by the client or both) is unnecessary overhead and also may indicate additional testing, since it can be error prone.

  • I don't understand the downvote for this. – Derek Elkins Sep 29 '17 at 21:37
  • 1
    don't try and understand the votes. that way lies madness – Ewan Sep 30 '17 at 18:00
  • 1
    The downvote was simply because your answer made an arbitrary assumption. The rules may not signal violations. They may signal validations. In such a case an empty set is probably considered false. Additionally, Bart van Ingen Schenau pointed out that an empty rule set could be an invariant violation specific to the program. I could not remove the downvote. – Frank Hileman Oct 3 '17 at 0:41
  • @FrankHileman, thanks for the comment. I did make an assumption. I tried called it out explicitly with the "If ...", though did not cover alternatives. – Erik Eidt Oct 3 '17 at 2:25

That depends on the possible reasons why the ruleContainer could be empty and the requirements on the validator class.

If an empty ruleContainer should not happen (and thus indicates a problem in the program), then throwing an exception would be best.

If the validation should pass if all rules pass, then it would be most logical to consider that all 0 rules of an empty container all pass and thus the validation passes.

I can't think of the top of my head of a case where an empty container should mean that the validation fails, but I can't rule out that someone else can. In that case, it should be clear from the requirements that the validation should behave in that way.

Not the answer you're looking for? Browse other questions tagged or ask your own question.