How To Write Comments In Elixir

Image for post
Image for post

TL;DR

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 #.

Remember

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 @moduledoc and @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.

Written by

Founder of Tentamen, software testing agency.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store