# Standard Library

## Summary


Every programming language defines a basic set of components that can be reused by programs. Every implementation of the language should provide these components. This set of reusable components is what we call a **standard library**. 

A good standard library is essential to the long-term success of a programming language. However, there's no consensus on what or how much should be included in a language's standard library. Python and Java have large standard libraries while C takes a minimalistic approach.  

The definition of a standard library is not limited to languages. It can be extended, for example, to frameworks. Thus, we can say that Django (web framework) and Robot Framework (test automation framework) have their own standard libraries that are installed by default with the framework.

## Discussion

### What should a standard library typically include?

Since the C standard library is minimal, it gives us an idea of what's typically included. These are either macro constants or functions. In an object-oriented language such as C++, classes, class templates and methods are part of the standard library. Others (Java, PHP) may additionally include interfaces.

Operations that are deemed necessary for most programs are included. A standard library will have functions to access the file system and perform input/output operations; functions that deal with the data types of the language; functions that allow memory allocation and manipulation; functions that enable multithreaded programming; functions to handle date and time; common math functions; functions for error handling and assertions. 

Modern languages have lot more than what C standard library offers. C++ has classes for containers, algorithms and regular expressions. Python has packages for data persistence, data compression, cryptography, exception handling, networking, CSV/XML parsing, GUI, testing, profiling, and more. 


### What are some advantages of using a standard library?

Developers can focus on their application logic rather than low-level details, which are provided by the standard library. Code can be reused across projects. Code is more maintainable since most developers will be familiar with the standard library. There's also a shorter learning curve for new developers joining a project.

Since implementations of the standard library are optimized and well tested, programs that use them are likely to be more reliable. If the language is open source, developers can also study the implementation of the standard library to learn best practices. 

Projects that use the standard library might even be smaller since developers are not introducing custom code for some modules while using the standard library in others.


### How are standard libraries delivered or distributed?

A standard library is not simply one physical document but rather a combination of many things:

    + **Standards document**: This is the main document released with every version of the language. This will include core parts of the language plus what's in its standard libraries. For example, C and C++ standards documents are released by ISO and must be purchased. In other languages, these may be more readily available online.
    + **Header files**: This is typical of C and C++. Header files specify what's available in the standard libraries.
    + **Implementations**: When a language compiler or interpreter is installed on a machine, an implementation of the standard library is also installed. In Python, users have a choice of different implementations of the standard library, with CPython being the default.
    + **Delivery**: In C and C++, implementations are object files. In Python, they are packages containing modules. In Java, we have packages containing classes. In client-side JavaScript, implementation is built into the web browser.Documentation and header files may also be termed as the Application Programming Interface (API) of the language. It's also common for vendors to include useful non-standard functions at the expense of portability. 


### Who should read standard libraries?

There are a few types of folks who need to read them: compiler writers, those who implement standard libraries, tool vendors, and users of the standard libraries.

Standards documents are usually hard to read because they are formal, legal and highly detailed. They are meant for compiler writers and those who implement standard libraries. Tool vendors may also read them. For everyone else, it's usually sufficient to read the documentation on how to use the standard libraries in their code. Tutorials and example code showing the use of standard libraries are more useful than reading the language standards.


### What are some considerations when defining a standard library?

What a standard library provides should fulfil the needs of programmers. In 2016, when initiating a revision of C standard, convener David Keaton pointed out that security threats on the Internet are growing, Moore's Law is failing and there's a need to exploit concurrency and parallelism in programs. Languages must therefore evolve to address these new concerns. 

Standard library must balance stability and evolution. Design should address safety, security, simplicity, interoperability, demanding application domains, and embedded systems. To add a feature, we must consider how it would interact with other features. Updates to the standard library must be backward compatible to earlier versions. While there could be some platform-dependent features, portability should be improved. Sometimes portability may be sacrificed for performance, at least in C. 

One point of view is that most standard libraries lack features and they are incompatible across languages. What if you could write a library in one language and automatically translate it into any desired language? **Loyc Multi-Language Standard Library (MLSL)** aims to do this. 


### Should a standard library be exhaustive or minimal?

C and C++ are minimal. Java provides many higher level utilities out of the box. While this benefits developers, it also bloats the language. Some APIs become antiquated and less useful, though they would have to be maintained unless deprecated. Python also adopts the "batteries included" approach and provides many sophisticated packages as part of the standard library. While this is useful, it also means that new implementations have more work to do. A small library implies that multiple implementations are possible and all programmers can rely on it. 

There's been some debate about moving C++ towards a "batteries included" approach, such as adding a graphics library. Alternatively, C++ can be kept minimal while finding a better way to distribute libraries and dependencies. 

The choice that language designers make must be governed by the its main purpose. Should it a general purpose language or satisfy a niche? It's been said, 

> No language can be everything for everybody.


### Could you list some examples of standard libraries?

Here's a short and non-exhaustive list of some standard libraries:

    + C: assert, float, math, stdio, stdlib, string, threads
    + C++: list, map, deque, vector, algorithm, iterator, string, regex, fstream, iostream, exception
    + Python: list, dict, tuple, range, open, enum, collections, math, pickle, zlib, threading, queue, email, json
    + Go: tar, gzip, heap, crypto, hash, image, io, mime, net, os, path, strings, time, unicode
    + Java: java.lang, java.io, java.net, java.math, java.text, java.awt.image, java.security, java.beans

### What happens if commonly required functions are not part of a standard library?

There's nothing in C standard library to help a developer send emails from a C program. The developer is forced to build this feature from low-level networking constructs. Moreover, that code can't be reused by others since it's not part of the standard library.

This is where the power of the community becomes important. Useful functions that are not part of the standard library are curated, reviewed and shared via public repositories. In Java, there's Apache Commons. In Python, there's PyPI. For C++, Boost provides peer-review libraries, some of which were later accepted into the standard library. For C, there's POSIX standard library that enables portability across different variants of UNIX/Linux while adding many functions missing in C standard library. 

C++ Boost binaries can be downloaded and installed for your platform. Modern languages implement the concept of package/dependency management. Software is gathered into centralized repositories and tools exist to manage packages and version dependencies as necessary for your project. Example repositories include Packagist (PHP), PyPI (Python), CPAN (perl), NPM (Node.js), and Maven (Java). 


### Are there well-defined processes to evolve the standard libraries?

Languages that are proprietary (such as MATLAB or Wolfram) will have closed processes. Customers of these products may request features. Otherwise, they have little control on how the language or its standard library evolves. On the other hand, more open languages are standardized by a process that allows greater community participation.

Both C and C++ are standardized by the International Organization for Standardization (ISO). Meetings are organized regularly to discuss changes to the language. Participants are organized into working groups. Proposals are submitted and refined to produce working drafts. Voting by members will promote these drafts to becoming part of the standard library. Those who wish to make proposals, should study the Standard Library Guidelines.

In the case of Python, the Python Software Foundation (PSF) does regular releases. Any modification to the standard library is done via a Python Enhancement Proposal (PEP). Proposals are discussed and documented in draft stages. Once finalized, they're implemented. 

Java has the Java Community Process (JCP) and proposals are managed as Java Specification Requests (JSRs). PHP has an RFC process with clear guidelines on how to participate. 

## Milestones

1989

The **ANSI C** standard library is standardized to overcome compatibility problems across variations and implementations of the C language. Effort towards this important milestone started back in 1983 when the American National Standards Institute (ANSI) formed a committee. This standard is also called **C89**. Normative Addendum 1 (NA1) is added to it in 1995. 

Sep  
1999

With `integer` library as its first entry, an experimental release of **Boost** is introduced for C++. 

Apr  
2012

As ISO/IEC 30170:2012, **Ruby** is published as an ISO standard. 

2017

ISO standardizes **C++17** for the C++ language. C++ was first standardized in 1998. The next planned evolution is named **C++20**, to be released in 2020. The C++ Standard Library was previously called Standard Template Library (STL).