The Stacks
BIG IDEA 3: ALGORITHMS AND PROGRAMMING · TOPIC 3.14

3.14 Libraries

Libraries are collections of procedures written by someone else. Using them well means reading documentation and understanding what an API promises.

What you need to know

  • A software library is a collection of procedures that can be used in other programs — often written and tested by other developers.
  • Benefits: save development time, reuse code that's already been debugged, and gain capabilities (graphics, networking, math) you'd otherwise have to build yourself.
  • An API (application programming interface) specifies how a library's procedures are called: their names, parameters, and what they return. It's the contract between your code and the library.
  • Documentation is essential for using a library correctly — it explains what each procedure does, what it needs, and how it behaves in edge cases. You can't use a library well without it.
  • Using a library is procedural abstraction in practice: you call sort(list) without knowing the sorting algorithm inside.
  • Libraries are typically imported into a program before their procedures can be called.

Worked example

Suppose a math library's documentation says: ROUND(x, places) returns x rounded to the given number of decimal places. You never see the rounding code; you just call ROUND(3.14159, 2) and get 3.14. If the documentation had said the second parameter is the number of significant figures, the same call would return 3.1. Reading the API, not guessing, is the skill.

Exam tip: Questions here are usually scenario-based: a developer wants to add a map to an app. The correct reasoning is "use a library to avoid rewriting tested code," and the correct next step is "read its documentation/API." Any option suggesting you must understand the library's internal implementation is wrong.

Going deeper

The nuance, edge cases, and connections that turn a 3 into a 5.

  • A library is a collection of procedures (and sometimes data) that other programs can use. The procedures were written, tested, and documented by someone else — that's the value: you don't redo their work.
  • An API specifies how to use a library: the procedure names, what to pass in, what comes back. You program against the API. The implementation behind it can change without your code changing, as long as the API stays the same.
  • Documentation is the CED's emphasis: you can't use a library correctly without reading how each procedure behaves — what it expects, what it returns, what happens in edge cases. A question describing a developer who "read the documentation to understand the parameters" is describing correct library use.
  • Libraries make development faster (don't rewrite tested code) and programs more reliable (widely-used libraries have had their bugs found). They also give access to capabilities you couldn't build yourself — graphics, networking, machine learning.
  • Using a library is procedural abstraction at scale: you call sortList(list) without knowing whether it uses merge sort or quicksort. The abstraction is the whole point.
  • Libraries are also a collaboration mechanism (1.1) — the people who wrote the library are, in effect, on your team.

Mistakes that cost points

  • Assuming you need the source code. You use a library through its API. Options saying "the developer must understand the library's implementation" are wrong.
  • Claiming libraries guarantee correctness. They're usually well-tested, but calling them with the wrong arguments still produces wrong results — hence documentation.
  • Confusing a library with a language feature. Built-in operators (+, MOD) aren't library calls. A library adds procedures beyond what the language provides.

Practice questions

Written in the style of the real exam. Try each one before revealing the answer.

Q1 A programmer wants to display a bar chart in their program. Rather than writing the drawing code from scratch, they use a charting library. Which of the following is the most important reason this approach is effective?
  1. A The library's procedures have already been developed and tested, saving time and reducing errors.
  2. B Programs that use libraries are always shorter.
  3. C Libraries eliminate the need for documentation.
  4. D Using a library guarantees the program has no bugs.
Show answer

Answer: A. Reusing tested code is the core benefit. Libraries don't guarantee bug-free programs or remove the need for documentation.

Q2 Which of the following best describes an API?
  1. A A type of lossy compression
  2. B The set of procedures, their parameters, and return values that a library makes available for other programs to call
  3. C The internal source code of a library
  4. D A list used to store data
Show answer

Answer: B. An API is the interface — what you can call and how — not the implementation.

Key vocabulary

Software library
a collection of pre-written procedures that other programs can use
API
the specification of how to use a library: its procedures, parameters, and return values
Documentation
written explanation of how a library's procedures behave