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.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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.