Subscribe to access all episodes. View plans →

#176: Elixir Styler

Published December 22, 2023

Elixir Styler

Credo and Styler

Follow along with the episode starter on GitHub


In episode 175 we covered Credo, a library that runs static code analysis on your project and suggests ways to improve it. While Credo is a great tool to help you improve your Elixir code and learn, sometimes you want a tool that will find ways to improve your code and Fix-It-For-You.

Enter Adobe’s Elixir Styler. Styler is an Elixir formatter plugin that is a little bit of mix format and mix credo. It identifies issues and formatting improvements in your code and fixes them for you.

Let’s see how Styler works. I have a little Elixir application that if we go to the command line and start an IEx session with our application. Our application here has a module Teacher that has two functions: one for generating long passwords and shorter passwords.

$ iex -S mix
Interactive Elixir
> Teacher.long_pass()
"fantastic bucked surely unwarlike runaround sulfite customs constrain"
> Teacher.short_pass()
"latch grunt stout slit"

Let’s open up the project in our editor and go to the teacher.ex module. Now there are some more obvious errors like our functions are not properly indented and there’s a blank line at the top of our module. But there are some less obvious issues, like Logger.warn is being deprecated in Elixir 1.15.

Let’s see how Styler can improve this code for us. We’ll go to Hex….and copy the styler package config. Then let’s open our project’s Mixfile and add styler to our list of dependencies. We’ll only need styler in our development and test environments, so let’s restrict it to that and we don’t need styler at runtime, so let’s include runtime: false.

mix.exs

...
defp deps do
  [
    ...
    {:styler, "~> 0.11.1", only: [:dev, :test], runtime: false}
  ]
end
...

Then let’s go to the command line and run mix deps.get to download styler.

$ mix deps.get
...
New:
  styler 0.11.1

Since Styler is an Elixir formatter plugin, let’s open .formatter.exs and add the plugins option, which takes a list of formatter plugin modules. We’ll just include one, the Styler module.

.formatter.exs

[
  ...
  plugins: [Styler]
]

With that Styler should be configured to update our project code for us. All we need to do is go to the command line and run $ mix format.

$ mix format
...

Now let’s open our module and see what changes it makes. Great, we can see it made some changes to the module. It fixed our indentation and blank lines, but it also added the @moduledoc module attribute for us. It also removed the empty parentheses from both of our short_pass and long_pass functions.

Styler made life a little easier for us by helping to keep us ahead of deprecation warnings - because Logger.warn is deprecated, it re-wrote it to Logger.warning. It also re-wrote our Map.merge functions which had a single key/value, to Map.put and updated the format of the arguments. Styler is opinionated and does make changes based on those opinions, but it does a great job of saving you time and really helps to keep your codebase consistent. If we make any additional updates to our .formatter.exs, like updating the line_length.

.formatter.exs

[
  ...
  line_length: 65,
  plugins: [Styler]

]

And run mix format again.

$ mix format
...

Our line length was updated and we didn’t lose any of our other styler changes. One note if you’re using Credo and Styler - you’ll want to disable a few rules in Credo to avoid unnecessary checks in your CI. And if you’re unfamiliar with Credo or how to disable checks, I cover that in episode 175.

© 2024 HEXMONSTER LLC