The benefits of object-oriented programming using class invariants (a.k.a. representation invariants)by Philip Guo (contact info) June 2008
Programming with class invariants (a.k.a. representation invariants) is a great idea ...
(0 <= age < 18) and (birthdate + age == todays_date) and isLegalSSN(social_security_number) which means, in English, that a child's age must be non-negative and
less than 18 and must be accurately reflected by his/her birthdate,
and that he/she must have a valid social security number as defined
by the
class Child {
...
private void checkRep() {
assert (0 <= age < 18);
assert (birthdate + age == todays_date);
assert (isLegalSSN(social_security_number));
}
}
class Child {
...
public void changeSSN(int newSSN) {
checkRep();
... method body ...
checkRep();
}
private void checkRep() {
assert (0 <= age < 18);
assert (birthdate + age == todays_date);
assert (isLegalSSN(social_security_number));
}
}
Related articles
Created: 2008-06-20
Last modified: 2008-06-20 |