FAQ 6.08 What are the properties of a substitutable (backward compatible) change in a specification?

Require no more, promise no less.

A change to an interface doesn't have to break existing code that uses the interface. If the new specification is substitutable with respect to the old specification, the user code will not break. Substitutable changes have two distinct properties. First, any user who fulfilled the old advertised requirements still fulfills the new ones (thus the new requirements must not get stronger). Second, any member function that fulfills the new advertised promises also would have fulfilled the old ones. In other words, existing user code must be adjusted if the replacement class requires users to do more and/or promises less than the original class did.

For example, Mac agrees to mow Lonnie's lawn for $10. Mac could substitute his friend, Patches, if the requirements went down (say $5) or if the promises went up (weeding the garden, for instance). However, Lonnie would be justifiably upset if Patches required more (say $20) or promised less (to mow only half the lawn, for instance).

In the following example, Version1 and Version2 represent subsequent versions of some class. Version2 is substitutable for Version1 since Version2 requires no more and promises no less than Version1 (in fact, Version2 requires less and promises more).

 class Version1 { public:   int f(int x);     // REQUIRE: Parameter x must be odd     // PROMISE: Return value will be some even number }; class Version2 { public:   int f(int x);     // REQUIRE: Parameter x can be anything     // PROMISE: Return value will be 8 }; 

For Version2 to be substitutable for Version1, every member function must be substitutable. That is, every member function for the new version must require no more and promise no less than the equivalent member function in the old version. If even one member function is changed such that it requires more or promises less, the entire class is not substitutable.



C++ FAQs
C Programming FAQs: Frequently Asked Questions
ISBN: 0201845199
EAN: 2147483647
Year: 2005
Pages: 566
Authors: Steve Summit

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net