C43 · Topic 3.14 · 40 minutes · AAP-3.D
Read a library contract
By the end you can:
- Extract argument, return and range requirements from documentation.
- Predict a documented library call.
- Design a test that distinguishes two implementations.
Before this lesson: topics 3.12, 3.13.
- Library
- A collection of procedures available for reuse.
- API
- An interface describing how software components can be used.
Understand
A library lets a programmer call tested procedures without rewriting their implementation. Documentation must describe argument types, order, allowable ranges, return values and error behaviour. A familiar name alone does not specify what a procedure does.
Library use is procedural abstraction: a caller depends on the documented contract. Reuse can reduce development effort, but a library may contain defects or be unsuitable. Test the behaviour your application relies on.
Worked example
Fictional library: clamp(value, low, high) accepts numbers with low ≤ high. It returns low below the interval, high above it, and value inside it. It does not display a result.
- clamp(-2,0,10) returns 0.
- clamp(6,0,10) returns 6.
- clamp(15,0,10) returns 10. The contract does not permit clamp(6,10,0).
Explore each concept
Choose a concept, change its inputs and follow the explanation. Each example models the stated rule or scenario; completing it is practice rather than evidence of mastery.
- Documented arguments, ranges and returns
- Implementation versus interface
- Integration and version tests
Change it · step through · explain
Change an input, predict what will happen, then advance one step. Read why the state changes, reset and experiment. This fixed teaching model does not save progress or award XP.
Ready · Make a prediction
- Value (0–10)5Waiting
- Minimum (0–10)5Waiting
- VersioncompatibleWaiting
Ready to explore: Library contract and compatible replacement
Why: Change an input, predict the first effect, then advance one step.
Try this: Change one input or choose another listed case. Predict the effect, reset and compare the states and governing rule.
Step through and experiment
Change it · step through · explain
Change an input, predict what will happen, then advance one step. Read why the state changes, reset and experiment. This fixed teaching model does not save progress or award XP.
Ready · Make a prediction
- Input 1 A B Waiting
Ready to experiment
Why: Predict the first change, then advance one step.
Try this: Try internal double spaces, only spaces and non-ASCII letters.
Check a misconception
Claim: Calling an external procedure means its code is automatically correct for my purpose.
The contract may not match your purpose, and the implementation still needs appropriate integration tests.
Guided practice
A student writes score ← clamp(12,0,10) + 2. Predict score and explain the returned value.
Work with a partner. Make a prediction, compare your reasoning, then identify the rule that supports your answer.
Independent transfer
Choose tests that distinguish clamp from a procedure that always returns high.
Exit check
Name two documentation details needed before replacing your own procedure with a library call.
Write your reasoning before comparing with a classmate. If your teacher assigned this task, submit it through My classes to save your work and receive feedback.
Check yourself
Your practice so far
Check an answer to see its rationale.