|
| 1 | +# About |
| 2 | + |
| 3 | +Elixir documentation: |
| 4 | + |
| 5 | +- A first-class citizen. |
| 6 | +- Written in [**Markdown**][markdown]. |
| 7 | +- Added by using special module attributes. |
| 8 | +- Typically uses the heredoc syntax for multiline strings. |
| 9 | + |
| 10 | +Module attributes used for documentation: |
| 11 | + |
| 12 | +- `@moduledoc` - describes a module, appears on the first line of the module. |
| 13 | +- `@doc` - describes a function, appears right above the function's definition and its typespec. |
| 14 | +- `@typedoc`- describes a custom type, appears right above the type's definition. |
| 15 | + |
| 16 | +```elixir |
| 17 | +defmodule String do |
| 18 | + @moduledoc """ |
| 19 | + Strings in Elixir are UTF-8 encoded binaries. |
| 20 | + """ |
| 21 | + |
| 22 | + @typedoc """ |
| 23 | + A UTF-8 encoded binary. |
| 24 | + """ |
| 25 | + @type t :: binary |
| 26 | + |
| 27 | + @doc """ |
| 28 | + Converts all characters in the given string to uppercase according to `mode`. |
| 29 | +
|
| 30 | + ## Examples |
| 31 | +
|
| 32 | + iex> String.upcase("abcd") |
| 33 | + "ABCD" |
| 34 | +
|
| 35 | + iex> String.upcase("olá") |
| 36 | + "OLÁ" |
| 37 | + """ |
| 38 | + def upcase(string, mode \\ :default) |
| 39 | +end |
| 40 | +``` |
| 41 | + |
| 42 | +## [Documentation vs. code comments][documentation-vs-comments] |
| 43 | + |
| 44 | +Elixir treats documentation and code comments as two different concepts. |
| 45 | + |
| 46 | +Documentation is an explicit contract between you and the users of your public API (which also includes your coworkers and your future self). Those users might or might not have access to the source code, so the documentation explains how to use your code. |
| 47 | + |
| 48 | +Code comments are aimed at developers reading the source code. They are useful for leaving notes, explaining implementation details, marking opportunities for improvement, and so on. |
| 49 | + |
| 50 | +Because documentation is meant to describe the public API of your code, trying to add a `@doc` attribute to a private function will result in a compilation warning. For explaining private functions, use code comments instead. |
| 51 | + |
| 52 | +``` |
| 53 | +warning: defp do_check_length/2 is private, @doc attribute is always discarded for private functions/macros/types |
| 54 | + lib/form.ex:33: Form.do_check_length/2 |
| 55 | +``` |
| 56 | + |
| 57 | +## Consuming documentation |
| 58 | + |
| 59 | +There are many different ways to access the documentation of an Elixir project. |
| 60 | + |
| 61 | +### `hexdocs.pm` and `ExDoc` |
| 62 | + |
| 63 | +[`hex.pm`][hex-pm] is a package repository for Elixir and Erlang. Every package published to `hex.pm` will get its documentation automatically published to [`hexdocs.pm`][hexdocs-pm]. There, you can find the documentation for all your favorite Elixir libraries, as well as [Elixir's official documentation][official-documentation] itself. |
| 64 | +
|
| 65 | +You can generate a documentation website for your project that looks exactly like Elixir's official documentation without having to publish a package to `hex.pm`. The tool that does it is called [`ExDoc`][ex-doc]. `ExDoc` will produce HTML files that you can browse from your local filesystem. |
| 66 | + |
| 67 | +Make sure to follow the [official recommendations for writing documentation][writing-documentation-recommendations] to ensure best results when using `ExDoc`. |
| 68 | + |
| 69 | +### The `h` IEx helper |
| 70 | + |
| 71 | +You can access the documentation of the standard library, as well as any library you have installed and your Elixir project, directly from your computer. |
| 72 | + |
| 73 | +If you have Elixir installed on your computer, you can use it in [the interactive mode][getting-started-iex] by running the `iex` command (or `iex -S mix` if you want to load the source of your current mix project). |
| 74 | + |
| 75 | +In `iex`, you can type [`h`][iex-h], followed by a space and a module name or a function name, to read its documentation. |
| 76 | + |
| 77 | +```elixir |
| 78 | +iex()> h String.upcase/1 |
| 79 | +# def upcase(string, mode \\ :default) |
| 80 | +# |
| 81 | +# Converts all characters in the given string to uppercase according to mode. |
| 82 | +# (...) |
| 83 | +``` |
| 84 | + |
| 85 | +By pressing the tab key after providing a partial module or function name, you can leverage the autocomplete option to discover available modules and functions. |
| 86 | + |
| 87 | +### Modern IDEs |
| 88 | + |
| 89 | +Many modern IDEs that support Elixir can parse and display documentation and typespecs in useful pop-ups, for example [Visual Studio Code][vsc-documentation] or [Intellij with the Elixir plugin][intellij-elixir-documentation]. |
| 90 | + |
| 91 | +## Internal modules and function |
| 92 | + |
| 93 | +If a module or a function is intended for internal usage only, you can mark it with `@moduledoc false` or `@doc false`. Those modules and functions will not be included in the generated documentation. Note that that doesn't make them private. They can still be invoked and/or imported. Check the [official documentation about hiding internal modules and functions][hiding-internal-modules-and-functions] to learn about potential solutions to this problem. |
| 94 | +
|
| 95 | +[markdown]: https://docs.github.com/en/github/writing-on-github/basic-writing-and-formatting-syntax |
| 96 | +[official-documentation]: https://hexdocs.pm/elixir/ |
| 97 | +[ex-doc]: https://hexdocs.pm/ex_doc/readme.html |
| 98 | +[hex-pm]: https://hex.pm/ |
| 99 | +[hexdocs-pm]: https://hexdocs.pm/ |
| 100 | +[writing-documentation-recommendations]: https://hexdocs.pm/elixir/writing-documentation.html#recommendations |
| 101 | +[intellij-elixir-documentation]: https://github.com/KronicDeth/intellij-elixir#quick-documentation |
| 102 | +[vsc-documentation]: https://thinkingelixir.com/elixir-in-vs-code/#Documentation_displayed_on_hover |
| 103 | +[iex-h]: https://hexdocs.pm/iex/IEx.Helpers.html#h/1 |
| 104 | +[getting-started-iex]: https://elixir-lang.org/getting-started/introduction.html#interactive-mode |
| 105 | +[hiding-internal-modules-and-functions]: https://hexdocs.pm/elixir/writing-documentation.html#hiding-internal-modules-and-functions |
| 106 | +[documentation-vs-comments]: https://hexdocs.pm/elixir/writing-documentation.html#documentation-code-comments |
0 commit comments