Concepts / Libraries and Standard Library Facilities

Libraries and Standard Library Facilities

A library takes over repetitive low-level work, while the program keeps the decisions about what the task requires.

  • Programming

Why a Simple Report Becomes Long

Imagine you have been asked to write a program that reads a list of student names, sorts them, and prints a neatly formatted report. You have a programming language and you know its basic ingredients: values, decisions, and repetition. You could start typing immediately. Within a few lines, you would notice that every small job has to be spelled out in full. Comparing two names character by character is a job. Padding a column so that the numbers line up is a job. Each of these must be written as explicit steps, and the program grows long before it does anything interesting.

This experience is the starting point for understanding libraries. Real software is not written in just a bare programming language. Before the work of any individual program begins, a set of libraries is usually developed, and those libraries become the foundation the program builds on. The rest of this section and the ones that follow explain what that foundation is, why it matters, and how a program's work changes once it depends on it.

What a Library Actually Is

Definition

A library is a collection of pre-written code that a program can reuse instead of writing every step itself.

The phrase pre-written is the key idea. A library does not change what your language can do. It gives you ready-made pieces that already perform a job. When your program needs one of those jobs done, it draws on the library rather than writing the same steps again. The library holds the pre-written steps, while the program holds the decisions about when and why those steps are needed.

What do you think happens?

Once your program imports a library, does the language itself suddenly gain new abilities?

  • Yes, the language gains new abilities

  • No, the library only supplies ready-made pieces that already do a job

Reveal answer
Answer:

No, the library only supplies ready-made pieces that already do a job

A library does not change what your language can do. It gives the programmer pre-written pieces, so the work that would otherwise be spelled out step by step is already handled inside the library.

Key point

A library takes over repetitive low-level work, while the program keeps the decisions about what the task requires.

Why Bare-Language Programming Is Tedious

The source states that most programs are tedious to write in the bare language. The reason is not that the language is useless. A language's basic facilities are general and small, so any moderately complex job breaks down into many low-level steps. A task that a person could describe in one sentence can require dozens of lines of careful instruction.

The same observation works in the opposite direction. Just about any task can be rendered simple by the use of good libraries. The claim is not that a library makes one task easy in isolation. It is that a good library can make nearly any task simple, because the tedious low-level steps have already been handled inside it.

Tracing Control Into a Library Routine

Follow one request from start to finish. Your program does not copy the library's code into its own text. Instead, it refers to what the library provides and asks it to do the job. The diagram below shows the order of those handoffs for the report task: the program passes the names to a routine, the routine performs the repetitive low-level steps, and the finished result comes back to the program, which then continues with the decisions it still owns.

Your programholds the decisionsLibrary routinepre-written stepsLow-level stepscomparing and padding1. asks routine to do the job with the names2. repetitive work is handled here3. low-level work completed4. finished result returned to program
When my program calls a library routine, which part does the repetitive work, and at what point does control return to my code?

Notice what stays in your program. The program still decides that the names need sorting, which routine to ask, and what to do with the result. Only the repetitive mechanics move out of your code.

How Work Shrinks When a Library Takes Over

Compare the two ways of building the same report. In the bare-language version, the program itself must contain every comparison, every swap that puts names in order, and every padding step that lines up a column. In the library version, those steps are no longer written in your program. The program names the jobs it needs and lets the library carry them out.

Worked example

Counting the Program's Own Steps

A report program must sort the student names, line up the columns, and print each row. Trace which parts the program must still write itself when it uses library routines for sorting and column formatting.

  1. 1
    Identify the repetitive jobs

    Sorting requires comparing and swapping names, and column alignment requires padding. Both are low-level and repetitive.

  2. 2
    Hand those jobs to the library

    The program asks a sorting routine for the ordered list and a formatting routine for the aligned columns, so those inner steps no longer appear in the program.

  3. 3
    Keep the decisions

    The program still decides which names go into the report, in what order to present the sections, and that each row must be printed.

  4. 4
    Count what remains

    The program now contains a few requests and the printing logic, while the detailed comparisons and padding live inside the library.

Result

The program's own work shrinks to the decisions and the printing, while the repetitive low-level steps are carried out by the library.

Layers Between the Language and Your Program

Software is usually organized in layers. At the bottom are the basic facilities of the language. Above them sit the library facilities that come with the language, which handle many common jobs. Libraries added by programmers sit above those, and your program draws on all of them. Each layer is built on the one beneath it, which is why the source describes significant programs as built on sets of libraries that form the basis for further work.

Your programdecisions about the taskLibraries added byprogrammersadded to the projectStandard libraryfacilitiescome with the languageBare languagefacilitiesvalues, decisions,repetitiondraws onbuilt onbuilt on
What contains what, and which parts come with the language versus which are added by a programmer?

Mistakes About When to Use Libraries

  • Common mistake

    Believing a library makes the language itself more powerful

    Why it fails

    A library does not change what your language can do. It supplies pre-written pieces that already perform a job.

    Do this instead

    Think of a library as ready-made work your program can call on, not as a new feature of the language.

  • Common mistake

    Assuming libraries are only useful for very large programs

    What it looks like

    Even a short report that needs sorting and column alignment benefits from a library, because those tasks would otherwise require many low-level steps.

    Why it fails

    The source says just about any task can be rendered simple by good libraries, and the tedious steps are present even in modest programs.

    Do this instead

    Consider a library whenever a task contains repetitive low-level work you would otherwise spell out in full.

  • Common mistake

    Copying library code into your own program and then maintaining it yourself

    Why it fails

    A program that uses a library does not copy the library's code into its own writing. It refers to what the library provides and asks it to do the job.

    Do this instead

    Request the job from the library and keep your own code focused on the decisions the task requires.

  • Common mistake

    Thinking the library takes over the whole task including the decisions

Bare Language Versus Library Approach

QuestionWriting every step in the bare languageDrawing on a library
Who performs the repetitive low-level stepsYour program, written out in fullThe library, which holds the pre-written steps
Who makes the decisions about the taskYour programYour program, which still decides what the task requires
How long the program tends to growLong, because every small job is spelled outShorter, because the jobs are requested rather than written
Effect on what the language can doNo change to the languageNo change to the language; ready-made pieces are added
When this approach fits bestWhen no suitable library job exists for the taskWhen a library already performs the repetitive job the task needs

Writing every step yourself versus drawing on a library for the same kind of task

Best practice

Before writing a long run of low-level steps, ask whether a library already performs that job. If it does, let the library carry out the repetitive work and keep the program's own code for the decisions about the task.

Checking Your Understanding of Libraries

EASY

A program must read a list of names, sort them, and print a report. Sorting and column alignment are handled by library routines. Which parts of the work does the program still need to decide, and which parts are carried out inside the library?

Hints
  • Look for the repetitive low-level steps such as comparing, swapping, and padding.
  • The program keeps the decisions about what the task requires, such as which names appear and in what order sections are presented.

Key Takeaways

  • A library is a collection of pre-written code that a program can reuse instead of writing every step itself.
  • Most programs are tedious to write in the bare language because its basic facilities are general and small, while good libraries can make nearly any task simple.
  • When a program draws on a library, the library takes over repetitive low-level work and the program keeps the decisions about what the task requires.
  • A library does not change what your language can do; it adds ready-made pieces that already perform a job.
  • Significant programs are built on sets of libraries that form the foundation for further work.

More concepts starting with L