Heuristics for finding class invariants [closed]
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 4 years ago.
Improve this questionIs there any heuristics for finding class invariants, I mean
- pay attantion on ...;
- never rely on ...;
Maybe there is common advices.
Any开发者_StackOverflow links on paper where studing real-life examples will be welcome.
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.
精彩评论