Changing a Function Contract Safely
A contract change can affect callers even when the function name and return type stay the same.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 4
- Reading time
- 1 min
In this article4 chapters
Find a behavioral dependency
A routine formerly returned zero for an empty input. Returning None now can cause a later addition to fail. Inspect callers and identify whether empty data means a zero amount or a missing measurement. Unset and zero serve different domain meanings.
Preserve a transition path
A separate function or explicit new option can offer the new behavior while existing callers migrate. Test earlier inputs under the default interface and add a case for the new outcome. Writing a function contract helps describe both promises for callers.[1][2]
Find observable changes beyond the parameter list
A function can keep accepting integer prices and still break callers by returning cents where it previously returned dollars. For a price of one hundred and twenty cents the old result is 1.20 dollars and the new result is 120 cents. A compatibility layer can expose an explicitly named cents operation and preserve old units during a transition. Test the same input against both entry points and include the unit in the expected result. See values with units.
Preserve the relevant meaning of failures and defaults
Changing a missing record from an ordinary result to an exception affects callers that previously checked one field. Adjusting a default page size affects clients that omit the argument. List each observable change and construct a test for both existing and new consumers. If both versions share a core implementation, their adapters can present the intended behavior independently. See default arguments as policy and error results and exceptions.