[go: up one dir, main page]

DEV Community

Sloan the DEV Moderator
Sloan the DEV Moderator

Posted on

Explain Like I'm Five: What's a standard library?

This is an anonymous question sent in by a member who does not want their name disclosed. Please be thoughtful with your responses, as these are usually tough posts to write. Email sloan@dev.to if you'd like to leave an anonymous comment or if you want to ask your own anonymous question.


Top comments (7)

Collapse
 
isaacdlyman profile image
Isaac Lyman • Edited

A standard library is a bunch of functions and types that are pretty much always included with a specific programming language. That is, if you're programming in the language, you can safely expect those things to be available to you.

This is different from actual features of the programming language. Adding numbers together with a plus sign 1 + 1 is not part of the standard library. But if you're programming in Python you'll have access to abs(), a function for getting the absolute value of a number, which is part of the standard library.

What's the difference? Technically abs() is not a unique or essential piece of Python syntax so you could create a Python implementation that doesn't have it. You'd get a bunch of angry programmers in your inbox, but it would still be Python! But if you created a Python implementation without the plus sign + infix operator, it wouldn't be Python at all.

The Python standard library is documented here. The JavaScript standard library, here. You can google "{language} standard library" to find documentation for whatever language you're working in.

Collapse
 
armousness profile image
Sean Williams

Others have provided good answers, but I'd like to expand on them a bit.

In C on Linux there are a couple of functions, mmap and malloc, which are used to claim exclusive access to some memory. (mmap has another use that it's more known for, but that's beside this point.) Both of these are part of what we might call the Linux standard library for C: mmap is in mman.h, and malloc is in stdlib.h.

C++, on the other hand, has a language keyword called new. For all intents and purposes, new and malloc do the same thing, but new is part of the language itself rather than coming in from a standard library.

Similarly, the C standard libraries have no real support for arrays that change size over their life—you can absolutely do this in C, but you have to do everything by hand. The C++ standard libraries provide a variety of automatically resizable collections, in the Standard Template Library.

I said all this to illustrate a point, which is... What ends up as language features, keywords and block structures and the like, is a series of decisions driven by the goals of the language designer. Typically one tries to keep the language features as minimal as possible while still being able to do what the language is supposed to do. C++ needs new to be a language feature because object instantiation is core to what C++ is. C does not need new because its type system is simpler than that.

What ends up in standard libraries is more like, a guess on the part of the language designers as to what users will need to be effective in the language. Nowadays this tends to include math (as Isaac pointed out), collections, memory management, string manipulation, network traffic, and on and on.

Collapse
 
theafr86 profile image
Andrew Robida • Edited

Anything you don’t have to npm install

Collapse
 
jsn1nj4 profile image
Elliot Derhay • Edited

Right, and Node and browser JS have different standard libraries, even though (for example) both Node and Chrome use Google's V8 engine.

There are features, classes, objects and functions that both have. But Node has some specifically for server-side operations (e.g. working with a filesystem and listening for network activity), and JS in a browser has some specific to web (e.g. for the DOM and browser window).

Collapse
 
theafr86 profile image
Andrew Robida

Lol I was trying to keep it simple.

Thread Thread
 
jsn1nj4 profile image
Elliot Derhay

Yeah, I realized after I posted that that it defeats the purpose of "explain like I'm five". 🤦‍♂️

Collapse
 
captainawesomedi profile image
Di Wu

Here is my attempt: A set of common (frequently used) functionalities curated by creators of the programming language, so you don't have to write from scratch