Documentation and edoc
A module that nobody can read is a module nobody can use. Erlang has two documentation forms that live in the source: the -moduledoc and -doc attributes of OTP 27, which the shell and the editing tools read from the compiled beam, and the %% comments that edoc turns into HTML. This lesson writes both, proves that the compiled module carries its docs, and shows the comment that looks like documentation but is not.
The doc attributes
-moduledoc describes the module and sits before the export list; -doc describes the next function. Both take a string, and OTP 27 writes triple-quoted strings, so a paragraph needs no escaping. The text is stored in the beam and the shell reads it with shell_docs:
greeting.erl
-module(greeting). -moduledoc """ Formats a greeting for one person. The module exists to show the documentation attributes of OTP 27. """. -export([main/0, hello/1]). -doc """ Returns a greeting for `Name`. The name is a binary, so the caller controls the encoding. """. hello(Name) -> <<"Hello, ", Name/binary>>. main() -> io:format("~s~n", [hello(<<"Ada">>)]).
erlc greeting.erl
erl -noshell -eval 'greeting:main(), halt().'
Hello, Ada
The doc text does not change what the function does. It changes what the shell can say about it: shell_docs:get_doc/1 answers the module doc as a list of paragraphs, which is how the shell builds the output of h/1:
erl
1> [{p, [], Texts} | _] = shell_docs:get_doc(greeting), Texts. [<<"Formats a greeting for one person.">>]
The p is a paragraph in the doc format, and the binary is the text. A tool that reads docs asks for the same value; the editor and the h/1 helper do not parse the source at all. The shell as a tool lesson shows h/1 at the prompt.
Edoc and the comments
edoc is older than the attributes and reads comments instead. A %% @doc comment directly above a function becomes the description of that function, and the module comment above the -module attribute becomes the module description. The tool writes a set of HTML pages into the directory named by {dir, ...}:
catalog.erl
-module(catalog). -export([main/0, total/1]). %% @doc Adds the prices of a list and returns the sum. %% The empty list costs nothing. total(Prices) -> lists:sum(Prices). %% @doc Runs the example with three sample prices. main() -> io:format("~p~n", [total([2, 4, 6])]).
erlc catalog.erl
erl -noshell -eval 'catalog:main(), halt().'
12
erl -noshell -eval 'edoc:files(["catalog.erl"], [{dir, "doc"}]), halt().'
ls doc
catalog.html edoc-info erlang.png index.html modules-frame.html overview-summary.html stylesheet.css
The pages are plain HTML with a stylesheet and an index, and the description of the function appears in the module page. A count proves that the comment reached the output:
grep -c "Adds the prices" doc/catalog.html
2
The two hits are the function index and the function details. The tool runs with the standard library only, so a project can build its docs in a build script with no extra dependency; the rebar3 lesson shows where such a step sits in a build.
A comment is not a doc
The mistake is a comment with one percent sign, or a %% comment separated from the function by a blank line. The first is an ordinary comment and the second attaches to nothing, so both tools report no documentation at all. This module has a comment that reads like a description:
undocumented.erl
-module(undocumented). -export([main/0, greet/0]). % This comment looks like documentation. greet() -> "hello". main() -> io:format("~s~n", [greet()]).
erlc undocumented.erl
erl
1> [{p, [], Other} | _] = shell_docs:get_doc(undocumented), Other. [<<"There is no documentation for ">>,<<"undocumented">>]
The shell synthesises a sentence that says exactly that: there is no documentation for this module. The function still runs, and shell_docs:get_doc/3 answers the same for the function. The source looks documented to a human and is undocumented to every tool, which is the worst of both.
What to write
Write the module doc as one sentence on what the module is for, then the parts a caller
needs: the shape of the main functions, the errors they return and the side effects
they have. Write the function doc for the caller, not for the author: what the
arguments mean, what comes back, and which failure is normal. The
exceptions lesson gives the error shapes
to name.
Use one form or the other inside a module. The attributes are what OTP 27 tools read,
and they are the form to write in new code; the %% @doc
comments are what edoc renders, so a project that publishes
HTML keeps them. A module that carries both is not wrong, but the two texts drift
apart, and a reader cannot tell which one the project treats as true.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does
-moduledocdescribe?Answer
The module
The attribute holds one string that describes the whole module.
-
What does a
-docattribute describe?Answer
The next function
The attribute sits directly before the function it documents.
-
Which tool turns
%% @doccomments into HTML?Answer
edocedocreads the comments and writes the HTML pages;shell_docsreads the attributes from the beam. -
Where does the shell find the text of
shell_docs:get_doc/1?Answer
In the compiled beam
The compiler stores the doc attributes in the beam, so the shell needs no source.
-
What does this module print?
-module(q1). -export([main/0, hello/1]). -doc "Returns a greeting.". hello(Name) -> <<"Hello, ", Name/binary>>. main() -> io:format("~s~n", [hello(<<"Ada">>)]).Answer
Hello, AdaThe doc attribute changes no behaviour;
hello/1builds and prints the greeting. -
What does this module print?
-module(q2). -export([main/0]). %% @doc Adds the prices. total(Prices) -> lists:sum(Prices). main() -> io:format("~p~n", [total([2, 4, 6])]).Answer
12The comment is documentation; the function still sums the list, and 2 + 4 + 6 is 12.
-
What does this module print?
-module(q3). -export([main/0, hello/1]). hello(Name) -> <<"Hello, ", Name/binary>>. main() -> io:format("~p~n", [hello(<<"Ada">>)]).Answer
<<"Hello, Ada">>The
~pdirective prints the binary with its<<"...">>form;~swould print the text alone. -
What does this module print?
-module(q4). -export([main/0]). -doc "Adds one.". add(X) -> X + 1. main() -> io:format("~p~n", [add(41)]).Answer
42A doc attribute is metadata; the function adds one to 41.
-
A comment with one
%above a function is what?Answer
An ordinary comment that no tool treats as documentation
%starts a plain comment; the doc forms are%% @docfor edoc and the-docattribute. -
Where does
edoc:files/2write the HTML?Answer
Into the directory named by
{dir, ...}The
diroption names the output directory; without it edoc writes into the current one.
The examples on this page were compiled and run with Erlang/OTP 27 (erts-15.2.7.14) on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev