Writing comments in Elixir is no brainer, the real skill is to know when we should use comments. This post is part of the functional language series, and it is based on the remarkable book Elixir In Action by Sasa Juric.
You write comments in Elixir same as in Ruby, using
#. You have an example in the image above. This is for human readers of your code,
elixirc will just ignore those lines that start with
When you use clarification comments, this could fail in two ways:
- Humans, including programmers and me, are error machines. The first problem with comments arise when code is updated, and the comment is not. Now we have out of sync between comment and code.
- Your code is not readable. Try to refactoring using hints from your clarification comments.
Comments are used for the automatic creation of code documentation. Now you have outdated documentation. As you learned, Elixir is using Module attributes
@doc for documentation. Is this better? For me, module attributes are more visible than code comments.
The best usage for comments is in the configuration code. That code does not change a lot, and it is not used for programming logic, but for setting your application.