Re: Handling class invariant violations
From: Alf P. Steinbach (alfps_at_start.no)
Date: 12/05/04
- Next message: Old Wolf: "Re: T (*)[N] and T**"
- Previous message: Rob Williscroft: "Re: > is printing as 62 and < is printing as 60"
- In reply to: DaKoadMunky: "Handling class invariant violations"
- Next in thread: Victor Bazarov: "[my OT] Re: Handling class invariant violations"
- Reply: Victor Bazarov: "[my OT] Re: Handling class invariant violations"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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?
- Next message: Old Wolf: "Re: T (*)[N] and T**"
- Previous message: Rob Williscroft: "Re: > is printing as 62 and < is printing as 60"
- In reply to: DaKoadMunky: "Handling class invariant violations"
- Next in thread: Victor Bazarov: "[my OT] Re: Handling class invariant violations"
- Reply: Victor Bazarov: "[my OT] Re: Handling class invariant violations"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|