Fl4m3Ph03n1x

Fl4m3Ph03n1x

What to document?

Background

I am now re-living the joys of documentation with elixir docs and when reading the documentation of mix docs I found this statement:

Documentation is an explicit contract between you and users of your Application Programming Interface (API), be them third-party developers, co-workers, or your future self. Modules and functions must always be documented if they are part of your API.

What does this mean?

So, for example, in my terminal app, the public API (the one visible to the customer) will be the CLI, which already has a helpful documentation. So, now I am done?

Something feels amiss here.

I usually document every module and inside the module I document the public functions. This means all modules are documented, even the ones the user will never see. But now I am having second thoughts.

Question

What is expected of a good documentation of a project?
What parts of the code should I document?

Marked As Solved

Qqwy

Qqwy

TypeCheck Core Team

Documentation exists at different layers, with different readers in mind.

  1. outward documentation: Explains to someone how to use your application or library as part of their project. This is in your example the documentation of the CLI so users understand how it can be used.
  2. Maintenance documentation: Explains how it fits together. This is documentation that allows others (including your future self) to understand how to adapt the application to changing future needs.
  3. Code comments: Explains why this approach was chosen, for the situations in which it is not immediately obvious. Examples are explaining what mathematical formula is the basis of numeric constants that would otherwise appear ‘magical’. Or that a certain less-readable implementation was chosen because of performance benefits, etc. Note that the how it works ought to be understandable from the code itself.

For most Elixir applications, (1) is part of e.g. a README or external guide, (2) is written using @doc, @moduledoc and @typedoc (people reading application documentation are the maintainers of the application) and (3) using comments next to the code they talk about.

For most Elixir libraries, (1) is written using @doc/@moduledoc/@typedoc (people reading library documentation are the users of the library) (2) as module- or function-level comments in a module with @moduledoc false and (3) using comments next to the code they talk about.

I disagree with @John-Goff; more documentation does not equal better documentation. On a related note, it takes effort to keep documentation up-to-date with the code; tools like Doctests are great to make this task a bit easier :slight_smile: .

10
Post #3

Also Liked

dimitarvp

dimitarvp

I go a lot of extra miles to make sure my code is self-explanatory – descriptive but short function names, descriptive parameter names, and hopefully giving a hint of the return value. I prefer a @spec to explain the contract of the function. Only if the function does something that cannot be immediately inferred from the spec and the names of it and the parameters, only then do I actually put a @doc.

Documentation is like code: the less of it you have, the less confusion.

John-Goff

John-Goff

More documentation is always better than less IMO. At the very least, you should document the public facing API. Beyond that, I typically do the same with a module doc and docs for all public functions. Sometimes I’ll use comments for private functions to document things for myself later that I don’t necessarily want to be included in the generated docs.

sergio

sergio

There’s no right or wrong here OP. Document as much as you can though, seven months later you will be thankful your functions have typespecs and docs with examples.

dimitarvp

dimitarvp

I’d say just go with more marketing-oriented material: rationale (what problem does the library solve), the why (should a programmer use it), the 2-3 common scenarios (extremely short snippets demonstrating usage that the most of the users would be interested in) and finally a quick roadmap with checkboxes on what’s done and what still isn’t (and maybe future plans).

baldwindavid

baldwindavid

It’s interesting to hear different opinions on this topic.

I struggle with the right amount of documentation to include. Mine is mostly a large, boring business application. A good bit of the modules include list_*, new_*, create_*, change_*, update_*, get_*, get_*!, and delete_*. I feel kind of bad about not including documentation for all of these functions, but it doesn’t seem overly useful unless there is something interesting going on in the implementation. It can also clutter the code and can easily get out of sync.

I also prefer a spec, especially if it can be enforced at runtime with typecheck or norm as it is does not get out of sync (assuming there is test coverage) and provides for consistent and clear documentation.

There are certainly cases where documentation is important for explaining the “why” or concepts the developer couldn’t know, but I haven’t felt compelled to add it for a large portion of my app. Maybe I’m doing it wrong.

I would certainly think about this differently for library code though.

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

We're in Beta

About us Mission Statement