views:

21

answers:

1

Hello everybody!

Is there any heuristics for finding class invariants, I mean

  • pay attantion on ...;
  • never rely on ...;

Maybe there is common advices.

Any links on paper where studing real-life examples will be welcome.

+1  A: 

classes vary so much from each other, heuristics will be hard to come by. There will be lots of heuristics for particular styles of classes though. For example, each design pattern implementation has its own set of invariants.

The best and simplest way to get your invariants is to start from a specification of what your class is supposed to do [or a test of tests if you do TDD], and figure out what conditions must be true for those specifications to be properly implemented. In other words, you "push your specifications through your code". As you do this, invariants will 'appear' as the stumbling blocks you encounter.

Note that sometimes "pushing through" means forward propagation of pre-conditions, sometimes it means backwards propagation of post-conditions. Most often, both.

Jacques Carette