Skip to main content
AP CSP course

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.

  1. clamp(-2,0,10) returns 0.
  2. clamp(6,0,10) returns 6.
  3. 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
Open this example on its own

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

Ready to explore: Library contract and compatible replacement
  1. Value (0–10)5Waiting
  2. Minimum (0–10)5Waiting
  3. 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

Ready to experiment
  1. 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

  1. Question 1 of 2The documented call clamp(14,0,10) returns which value?

    Select one answer.

  2. Question 2 of 2Which TWO practices support reliable library use?

    Select exactly two answers (0 of 2 selected).

Your practice so far

Check an answer to see its rationale.

Teacher guidance and scoring