Libraries
What a software library is, what an API tells you, why documentation matters, and how borrowing tested code makes big programs easier to build.
1Borrowing code that already works
A software library is a collection of procedures that can be used in creating new programs. Someone wrote them, tested them, and packaged them so other programs can call them. When a program needs a square root, a sort, a map, or today's weather, it usually calls a library instead of writing that code again.
Using libraries makes complex programs easier to create. You write less code, you test less code, and the borrowed parts have already been checked by many other programs. A library saves the programmer's effort; it does not automatically make the program run faster.
2Where existing code comes from
Existing code segments can come from internal or external sources:
| Source | Examples |
|---|---|
| Internal: code you or your team already wrote | a procedure from last month's project; a shared folder of your company's tested procedures |
| External: code from outside | a library that comes with the programming language; a free library someone published online; a map or payment service's library |
Either way, the code was written and tested before your program needed it. When you use code someone else wrote, give credit to its author and follow the rules (the license) it was shared under.
3The API and its documentation
An API (application program interface) is a specification for how the procedures in a library behave and how they can be used: each procedure's name, what to pass in and in what order, what comes back, and what happens in unusual cases. The API is written down in the library's documentation.
Here is the documentation for a small library of list procedures used on the rest of this page:
| Procedure | Takes | Returns | Edge cases |
|---|---|---|---|
largest(aList) | a list of numbers | the largest value in the list (the value, not its position) | an empty list causes an error and the program stops |
average(aList) | a list of numbers | the total divided by how many numbers there are; may be a decimal | an empty list causes an error and the program stops |
countAbove(aList, limit) | a list of numbers, then one number | how many items are greater than limit (items equal to it are not counted) | returns 0 for an empty list |
Documentation is necessary, not just helpful. You usually cannot see the library's code, and even if you could, reading it would cost the time the library was supposed to save. The documentation is how you learn that largest returns a value and not a position, or that countAbove skips items equal to the limit.
4Same job, two ways
A teacher wants the top score, the average, and how many scores are above 80. With the library, the program is 4 lines:
scores ← [72, 88, 95, 61, 84] DISPLAY(largest(scores)) DISPLAY(average(scores)) DISPLAY(countAbove(scores, 80))
Without it, the program has to do all of that work itself, in 19 lines:
scores ← [72, 88, 95, 61, 84]
best ← scores[1]
total ← 0
count ← 0
FOR EACH s IN scores
{
IF (s > best)
{
best ← s
}
total ← total + s
IF (s > 80)
{
count ← count + 1
}
}
DISPLAY(best)
DISPLAY(total / LENGTH(scores))
DISPLAY(count)
Both programs display 95 80 3. The library version is shorter, easier to read, and leaves fewer places for a mistake to hide, because the work inside largest, average, and countAbove was already written and tested. This is procedural abstraction on a bigger scale: you use what a procedure does without needing to know how it does it.
A library is a collection of tested procedures to use in new programs, from internal or external sources. Its API specifies how each procedure behaves and how to call it, and the documentation is how you learn that. Libraries make complex programs easier to create.
5When you guess instead of reading
A student guesses that largest returns the position of the largest score and writes:
scores ← [72, 88, 95, 61, 84] top ← largest(scores) DISPLAY(scores[top])
largest(scores) returns 95, the value. The program then asks for scores[95], and the list has only five items, so the program stops with an error: index 95 is out of range for a list of 5 items. One look at the documentation (“the value, not its position”) would have led to DISPLAY(top), which displays 95.
6Worked examples
Warm days
Using the documented procedures, what does this display?
temps ← [58, 64, 71, 69, 75, 80, 77] DISPLAY(countAbove(temps, 70)) DISPLAY(largest(temps))
- 1
countAbove(temps, 70)counts the items greater than 70. Check each one: 71, 75, 80, and 77 are; 58, 64, and 69 are not. The count is 4. - 2
largest(temps)returns the largest value, 80.
4 80.Edge cases
Using only the documentation table, predict each result: (a) countAbove([3, 8, 8, 10], 8); (b) average([10, 15]); (c) countAbove([], 5).
- 1(a) Only items greater than 8 count, so the two 8s do not. That leaves 10 alone: 1.
- 2(b) (10 + 15) ÷ 2 = 12.5; the documentation says the result may be a decimal.
- 3(c) The documentation says an empty list gives 0.
No ages yet
The documentation says average of an empty list causes an error. Rewrite this segment so it never stops with an error.
ages ← [] DISPLAY(average(ages))
- 1Check the list's length before calling the procedure.
- 2Only call
averagewhen there is at least one item:ages ← [] IF (LENGTH(ages) > 0) { DISPLAY(average(ages)) } ELSE { DISPLAY("no ages entered") }
no ages entered; with [15, 16, 17] it displays 16.Why use the library?
A classmate says: “Using a library is cheating, and it makes the program slower because it has to call other code.” Respond.
- 1Using tested procedures from a library is normal practice; the library exists to be used in new programs (with credit to its authors).
- 2The benefit is about building the program: less code to write and test, and fewer places for errors.
- 3A library is not automatically faster or slower; speed depends on the algorithms inside, not on whether the code was borrowed.
7Common mistakes
A name like largest doesn't say whether you get a value or a position. Read the documentation.
Documentation often lists inputs that cause errors, such as an empty list. Check for them before you call.
countAbove(80, scores) passes the number where the list should go, and the program fails. The documentation gives the order.
The benefit is easier development: less code to write and test. Speed depends on the algorithm inside.
“The program uses a library procedure, largest, instead of writing its own loop. The library's documentation describes what the procedure takes and returns, so the programmer can use tested code without reading how it works, which makes the program shorter and easier to build.”
Name the procedure, say what its documentation tells you, and say how that simplifies writing the program.