Re: Handling class invariant violations

From: Alf P. Steinbach (alfps_at_start.no)
Date: 12/05/04


Date: Sun, 05 Dec 2004 20:16:59 GMT


* DaKoadMunky:
>

Please use at least a pronouncable nick (or your real name, best).

> Say that an invariant for my class is that Foo::bar always be an even number.
>
> If somebody calls Foo::SetBar(int) [which does nothing but assign the argument
> value to Foo::bar] with an odd number what is the best way to handle it?

There is a mismatch between the interface you present to client code,
and the internal class invariant.

As it is, the caller of Foo::SetBar must use a redundant representation.

The simplest and best way to handle that problem is to decide whose job
it is to _reduce_ the redundancy to the required non-redundant
representation.

Is it your class? Then throw an exception (but contrary to advice given
elsewhere in this thread, don't use the standard's predefined exception
type for that, because it derives directly from std::exception; use
std::runtime_error or a derived exception class). Also then consider
adding a middleman argument type, e.g. EvenNumber, that can do this.

Is it the client code? Then change the interface. E.g. document that
SetBar will use twice the argument value (or whatever).

-- 
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


Relevant Pages

  • Re: How to tell caller of an error if you cant change the signature of a method
    ... runtime exception does the job for me. ... contract between me and the caller) should have stated something else. ... interface is, and the acknowledgment that that contract is by design. ... Throwing a runtime exception in turn is also a signal to client code of the ...
    (comp.lang.java.programmer)
  • 7.0 wishlist?
    ... Any object can be used where an interface type is expected if it has the right set of method signatures, whether or not it "implements" the interface using that keyword. ... the algorithm name is hard-coded and so this exception ... Most invocations of runnables ... Allow subclassing an enum with the subclasses able to REMOVE (not ...
    (comp.lang.java.programmer)
  • Re: Abstract Data Types - Separating Interface from Implementation
    ... >>You seem to be confusing implementation code and client code. ... I don't define any of the functions yet, I just declare them and compile ... Each such file defines an *interface*. ...
    (comp.lang.cpp)
  • Re: Objekt einfrieren?
    ... public interface Freezable { ... public class FrozenException extends RuntimeException { ... welches neben E auch noch Freezable ... fo.setX; // gibt eine Exception ...
    (de.comp.lang.java)
  • Re: Remote Debugging ASP.net 2.0 Web Service (no symbols loaded?)
    ... John Saunders [MVP] ... (either return values or one line error messages - typically exception ... this interface are somewhat ambiguous, ... information in the HTTP response from these web methods, ...
    (microsoft.public.vsnet.debugging)