Read The Fucking Manual
Tags: javascript, webdev, programming, opensource
Published: 2021-08-25 08:52:50

## The documented behavior is the only thing that matters.

----

Originally published on May 19, 2016

When we want to write code using a programming language, there is no need to learn the whole specification to understand its works. We read a few trusted resources, try it and see if we can get the desired outcome. If we can't, then we try it again until it works. The specification is used as a **reference**, not as **documentation**[1].

As a programmer, you have to understand this.

A programming language is hard to change. It will always thrive on being as backward compatible as possible. Authors of programming languages are usually conservative on what to remove, although there are [some cases where updates will break](https://bocoup.com/weblog/es2015-nightmarefile) anyway.

That is similar to frameworks. However, unlike languages, a framework can create a "version 2" that [rewrites everything](https://divshot.com/blog/opinion/angular-2-crazy-like-a-fox/). There will be a lot of work, but the responsibility of migrating lies on the consumer side. A framework is pluggable to some extent; a language is not.

As a programmer, you have to understand that too.

However, what happens when you plug a library into your app? A library is supposed to abstract a specific problem; it should be much more **open to new changes** than a framework. A framework dictates how you code, a library provides particular functionality that should be environment-agnostic. You can replace a library easily, a framework you can't.

In JavaScript, no access modifier can limit a given API's visibility; everything is public by default. It's possible to use [closures](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures) and [other techniques](http://dmitrysoshnikov.com/ecmascript/chapter-7-1-oop-general-theory/#encapsulation) to control access, but that might not be enough to [restrict](https://fagnerbrack.com/the-strictness-principle-9997e483cafb) it.

How can we make sure that a library's internal functionality will not be used by the developer that inspects the code using the browser console? How can we make sure the developer knows the contract that the library author provides for consumption without relying on trial and error?

We document it.

Just because it "works", that doesn't mean you're using the library correctly. The documentation, also known as the **Public API**, serves as a reference to verify if you are using the API correctly. The contract between the author and the consumer is the [single source of truth](https://en.wikipedia.org/wiki/Single_source_of_truth) — nothing else.

Everything else is internal, and therefore consumers should assume that it doesn't exist. Even if the author exposes it for convenience, it can be removed anytime without notice, and your app will break.

If the author follows [Semver](http://semver.org/), it is reasonable to remove an exposed (but undocumented) API in a minor or patch version. [Semver](http://semver.org/) states that the author should maintain backward compatibility **only for the public API**:

> For this system to work, you first need to declare a public API
> — semver.org

There are cases where authors might want to [create additional rules and restrictions](https://github.com/jscs-dev/node-jscs/blob/291bb4143d427eb488a75c6533f04a2937957d18/OVERVIEW.md#user-content-versioning--semver), depending on the library's purpose.

> ### For all practical purposes, what the author does not explicitly document in the public API does not exist.

Failing to read the docs of a library can cause the system to break because we will rely on how it behaves, not how it's intended to work. It is a recipe for ruining your app any time the patch version is updated instead of breaking only in a major version bump. Even if the system has proper tests, there is no guarantee.

Reading the docs seems so fundamental that one would believe it's common sense. However, I have met many developers that are not aware of the importance of reading the docs, and worse than that, nobody seems to talk about it very often. The lack of this kind of talk forces newcomers to learn the hard way through the experience of creating software that works, and then it doesn't.

> ### How can we make common sense more common?

Here's what happened to me when I started programming many years ago.

### jQuery

At the time of writing this, [jQuery](http://jquery.com/) has the $.trim utility function. It works like this:

> […] removes all newlines, spaces (including non-breaking spaces), and tabs from the beginning and end of the supplied string.
> — jQuery documentation

One day I was inspecting the jQuery source code. I noticed it was internally using `String.prototype.trim` [as the first option](https://github.com/jquery/jquery/blob/aa1350d9e29571a82d09bb7a2eac4396901daa77/src/core.js#L607-L620) if the native trim was available in the browser (probably due to performance reasons). Then it had a fallback for a [custom trim](https://github.com/jquery/jquery/blob/aa1350d9e29571a82d09bb7a2eac4396901daa77/src/core.js#L615-L620) that had the restricted documented behavior. `String.prototype.trim` does more than just removing spaces and tabs, so I thought it made sense matching the full spec and [even suggested it](https://github.com/jquery/jquery/pull/897#issuecomment-7828269). However, I received [feedback that it didn't make any sense](https://github.com/jquery/jquery/pull/897#issuecomment-7829005) because jQuery didn't document the API to replace more than mere spaces and tabs.

> The **trim()** method removes whitespace from both ends of a string. Whitespace in this context is all the whitespace characters (space, tab, no-break space, etc.) and all the line terminator characters (LF, CR, etc.).
> — String.prototype.trim on [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim)

If these three conditions were true:

1. The programmer used `$.trim` as a language, trying to make it work through trial and error.
2. The browser running the code supported the native trim method.
3. The app relied on trimming chars other than spaces or tabs for critical functionality.

Then the application would break once jQuery removed the `String.prototype.trim` fallback from the algorithm in a newer version.

> ### Using a library like a programming language will eventually break your system.

In some cases, fixing a bug can break many places because of an implied functionality or philosophy. Take the [hash selector breakage](https://github.com/jquery/jquery/issues/2824), for example, that was a legit bug fix that unfortunately broke an implied contract between a fundamental jQuery philosophy and big consumers of the library (such as WordPress). It's a great example because it showed that the assumption of what is implied between the library author and the consumer is not always clear and [can be up to interpretation](https://fagnerbrack.com/wittgenstein-s-beetle-in-software-engineering-dcea89a5db92).

### JavaScript Cookie / jQuery Cookie

I also happen to maintain a small library called [js-cookie](http://github.com/js-cookie/js-cookie). At the time of this writing, that library had some leftovers from [jquery.cookie](http://github.com/carhartl/jquery-cookie) code. I forked the original code to a new repository, and it carried over an undocumented API: `Cookies(String, String)`. However, the only [publicly documented APIs](https://github.com/js-cookie/js-cookie/tree/f2b25713e8fae658d418b60710adc0e0d14998d9#basic-usage) are `Cookie.get`, `Cookies.set` and `Cookies.remove`. In this case, because some folks didn't read the documentation, we had reports [like this one](https://github.com/js-cookie/js-cookie/issues/117) that showed examples of devs relying on the undocumented API.

The point is: always follow the docs. If there is an internal behavior that you want to depend on, request to update the docs so that the library authors can decide if that should exist as a supported feature or not.

There are cases, though, just like the hash change example from jQuery, where library authors have to fix an undocumented code. That usually happens due to an unexpected violation of the [Least Astonishment's Principle](https://en.wikipedia.org/wiki/Principle_of_least_astonishment) coupled with a massive amount of the wrong usage in the wild. [Read this thread, and you'll learn a great example](https://github.com/js-cookie/js-cookie/issues/145), more specifically [this comment](https://github.com/js-cookie/js-cookie/issues/145#issuecomment-205756498).

> ### Don't use an internal behavior; start with the docs.

There are some bugs programming language designers can't fix. The wrong usage at scale and the potential of breaking the web make it impossible for them to do so.

Frameworks dictate how you write the code. They should be conservative in what to break, even if it's a functionality that the framework authors haven't documented. However, once in a while, they can change everything so that progress doesn't stop.

JavaScript libraries are pluggable, and their internals is not easy to hide. Therefore they have the opportunity to support only what is publicly documented for the sake of extensibility. But there are exceptional cases in which one should be careful about what to expose.

As a dev, never rely on library internals. In practice, they don't exist.

----

**Edit April 28, 2017:**

Among the things that compose your library's public API, [method binding might be considered one of them](https://www.bennadel.com/blog/3254-method-binding-is-an-implicit-part-of-your-api-contract-whether-you-like-it-or-not.htm).

**Edit March 25, 2021:**

It's been four years since I've written this post, and most of it is still relevant today. Even with the widespread usage of [TypeScript](https://www.typescriptlang.org/) and the recent plans of introducing [Private class fields](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Private_class_fields) in the JavaScript ecosystem, the docs remain the accepted canonical source for any library use.

**Edit June 03, 2021:**

Jake Archibald recently wrote [this piece](https://jakearchibald.com/2021/function-callback-risks/) which also brings more clarity to function callback and its public API properties.

----

**1**: I’m using the word **reference** for the cases when you should consult the root specification to learn better about a given behaviour’s specifics. However, in those cases, you may not be required to read the whole specification to know how the system works. You might be using a library that abstracts away the specifics and consults the spec when [abstractions leak](https://fagnerbrack.com/repairing-the-leaky-abstraction-e726baab91b5), such as the case of [RFC 6265](https://tools.ietf.org/html/rfc6265) vs [js-cookie](https://github.com/js-cookie/js-cookie). Also, you might be using a type-safe language where you can learn the basics and then move to trial and error, such as the case of [Java spec](https://docs.oracle.com/javase/specs/) vs [Java lang tutorials](https://docs.oracle.com/javase/tutorial/).

I’m using the word **documentation** for how it works, including the basic “Get Started” examples. You have to read everything about the tool you’re using or at least the parts relevant to your case to start using it efficiently. Otherwise, you’ll begin with assumptions that are most likely to be wrong. For example, [Luxon](https://moment.github.io/luxon/), [jQuery](https://jquery.com/), and [React](https://reactjs.org/).

----


Thanks for reading. If you have some feedback, reach out to me on [Twitter](https://twitter.com/FagnerBrack), [Facebook](https://www.facebook.com/fagner.brack) or [Github](http://github.com/FagnerMartinsBrack).