Elixir
Format Your Elixir Code Now
Elixir 1.6 will be shipping with an important new feature: autoformatting. Let's explore it today.
Autoformatting is a common practice in languages communities like Go. Now, this practice is coming to Elixir as part of the language. Borrowing the arguments in favor of autoformatting from the Go community, autoformatted code ought to be:
- Easier to write: never worry about minor formatting concerns while hacking away.
- Easier to read: when all code looks the same you need not mentally convert others' formatting style into something you can understand.
- Easier to maintain: mechanical changes to the source don't cause unrelated changes to the file's formatting; diffs show only the real changes.
- Uncontroversial: never have a debate about spacing or brace position ever again!
Whether you agree with all these points or not, the Elixir language (which is largely written in Elixir) just underwent a massive community-powered refactor to conform with its new built-in formatter. New pull requests to Elixir must satisfy the formatter to be considered for acceptance. The Elixir community is behind the tool, and running it on your Elixir project is going to become the norm.
But we don't have to wait for Elixir 1.6. Let's try it now!
Install Elixir Master
There are a few ways to get the latest Elixir. If you use Homebrew, install Elixir from master:
$ brew install elixir --HEAD
$ elixir -v
Erlang/OTP 20 [erts-9.1.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]
Elixir 1.6.0-dev (425cebf) (compiled with OTP 20)
Alternately, build from source:
$ git clone git@github.com:elixir-lang/elixir.git
$ cd elixir
$ make clean compile
$ bin/elixir -v
Erlang/OTP 20 [erts-9.1.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]
Elixir 1.6.0-dev (425cebf) (compiled with OTP 20)
If mix help format
returns a man page, we're in business.
Format Your Code
Now that we have the formatter, let's use it:
$ mix format lib/path/to/file.ex
Or, run it against your entire program! To do this, we'll need a .formatter.exs
config file that
tells the formatter what to include. An example:
[
inputs: ["mix.exs", "{config,lib,test}/**/*.{ex,exs}"]
]
This will capture the Mixfile and Elixir files in config
, lib
, and test
.
With this file in the root of your project, format all the files:
$ mix format
Conclusion
Jose Valim's Elixir issue #6643 inspired this post. Check it out, and learn how to use the formatter in a variety of edge cases.
We're running the formatter right now against all the files in the Today I Learned codebase, starting with this pull request. Part of open-sourcing Today I Learned (first as a Rails app, now as a Phoenix app) means writing code that's as readable as possible. Complying with the Elixir formatter now helps us achieve that.
Try the Elixir formatter out today, and consider opening a pull request against an Elixir project you care about. Your proposed changes should spark an interesting debate about Elixir style and autoformatting as a practice.
Sources: