Libraries
| English | Chinese | Pinyin |
|---|---|---|
| library | 库 | kù |
| from scratch | 从头开始 | cóng tóu kāi shǐ |
| API | 应用程序接口 | yìng yòng chéng xù jiē kǒu |
| documents | 记录 | jì lù |
| functionality | 功能 | gōng néng |
| abstraction | 抽象 | chōu xiàng |
Reusing code others have written
- A library 库 is a collection of procedures you can reuse across programs.
- Rather than write everything from scratch 从头开始, you use code others have already written and tested.
- That saves time and reduces bugs.
- Almost every real program leans on libraries.
A library is:
It lets you reuse code instead of writing from scratch.
Reading the API
- To use a library well, you read its API 应用程序接口 — the application program interface.
- The API documents 记录 how to use the library: which procedures it offers, what arguments they take, what they return.
- You do not need to see the library's inside code.
- The API is the contract between you and the library.
Which library for the job?
Pick the library that matches your task: a maths library for numbers, a graphics library for drawing, a random library for random values.
A library's API tells you:
The API documents usage; you never need the inside code.
A well-chosen library saves time and reduces bugs because its code is already tested.
You reuse tested code instead of writing and debugging your own.
Adding functionality fast
- Libraries add functionality 功能 to your program quickly.
- A maths library gives square roots; a graphics library draws shapes; a random library gives random numbers.
- Pick an appropriate library for the part you need.
- A well-chosen library saves time and reduces bugs, because the code is already tested.
Calling sqrt(9) and getting 3 without knowing the method inside is an example of:
The library hides its implementation behind a simple call.
To draw shapes on the screen, you would choose a:
Match the library to the kind of job you need.
r ← sqrt(2) stores about 1.414, using a ______ library.
The maths library provides the tested square-root procedure.
Libraries and abstraction
- Like procedures, libraries support abstraction 抽象 by hiding complex implementation.
- You call
sqrt(9)and get 3 without knowing the method used inside.
Square root of 2. Instead of writing your own algorithm, call a maths library: r ← sqrt(2) stores about 1.414. The API tells you sqrt takes one number and returns its square root. The whole task becomes one line.
A library is reusable, tested code, so you don't build everything from scratch. Read its API to learn which procedures it offers and what they return — you never see the inside. Libraries add functionality fast and provide abstraction: sqrt(9) gives 3 without you knowing how.