Crowdhailer

Crowdhailer

Creator of Raxx

Running the new Elixir formatter

I’ve been hearing much about the new formatter and it’s something I have been keen to try.
I find examples buy far the most illuminating way to try some new tech. To that end I though I would blindly run the new formatter on my largest open source Elixir code base, Ace.

I did no preparation before hand and in one swoop was able to format the whole project using

$ mix format mix.exs "lib/**/*.{ex,exs}" "test/**/*.{ex,exs}"

The full change commit

Results

  • My project is now larger. 1369 lines added but only 752 removed.
    A lot of these are empty lines. Every case clause is separated by a new line and so is every function head.
    It looks like my personal convention of separating different functions but not different function heads of one function is now over
  • Brackets are now applied automatically so no more dealing with those warnings. This is a big win.
  • Numbers lose underscores 65_53565535 :frowning:
  • Several cases where things are now indented to line up with something above rather than just two spaces, this is probably what I am least happy about but I can get over it for the consistency
  • Comments now always end up on a separate line.

In conclusion I think that is a great additions and I hope that the numbers will eventually be formatted with the underscores

248 19328 150

Most Liked

NobbZ

NobbZ

Please don’t… I was born 1981, not 1_981…

11
Post #7
josevalim

josevalim

Creator of Elixir

You can have all of them on the same line if you want to:

case foo do
  {:ok, bar} -> {:ok, bar}
  {:error, baz} -> {:error, baz}
end

But if you have newlines after ->, then the formatter will respect that and proceed to add an empty line between each clause.

Remember the goal of the formatter is to make the code consistent. The opposite of the current behaviour would be to remove lines between clauses, which would affect different areas of @Crowdhailer’s repository. I think users would very likely be more upset if we remove the empty lines they added to space out code.

Between the two below, one is definitely more readable than the other for me (although I agree it is subjective):

defp integer_to_algebra(text) do
    case text do
      [?0, ?x | rest] ->
        "0x" <> String.upcase(List.to_string(rest))
      [?0, base | _rest] = digits when base in [?b, ?o] ->
        List.to_string(digits)
      [?? | _rest] = char ->
        List.to_string(char)
      decimal ->
        List.to_string(insert_underscores(decimal))
    end
  end

and

defp integer_to_algebra(text) do
    case text do
      [?0, ?x | rest] ->
        "0x" <> String.upcase(List.to_string(rest))

      [?0, base | _rest] = digits when base in [?b, ?o] ->
        List.to_string(digits)

      [?? | _rest] = char ->
        List.to_string(char)

      decimal ->
        List.to_string(insert_underscores(decimal))
    end
  end

We will open up a discussion on that.

chulkilee

chulkilee

# install erlang with homebre
brew install erlang

# install asdf to manage Elixir versions
if [ -d "$HOME/asdf" ]; then
  git clone https://github.com/asdf-vm/asdf.git "$HOME/.asdf" --branch v0.4.0
fi

# don't forget to add following part to your shell config (e.g. .bashrc)
. "$HOME/.asdf/asdf.sh"

# install elixir plugin if missing
plugins=$(asdf plugin-list)
if grep -q -v elixir <<< "$plugins"; then
  asdf plugin-add elixir https://github.com/asdf-vm/asdf-elixir.git
fi

# install with asdf as in .tool-versions
asdf install

# install master version
asdf install elixir ref:master

# run mix format with elixir master
ASDF_ELIXIR_VERSION=ref-master mix format

I have mix_format alias in my ~/.alias

alias mix_format="ASDF_ELIXIR_VERSION=ref-master mix format"

Where Next?

Popular in Discussions Top

Donovan
Hello everyone, I’m so glad to have discovered this awesome community. Thanks for creating it! This is my second post, and apologies for...
New
thojanssens1
It would be nice to be able to define a redirect from one route to another from the router.ex file. E.g.: redirect "/", UserController, ...
New
arpan
Hello everyone :wave: Today I am very excited to announce a project that I have been working on for almost 3 months now. The project is...
New
chuck
Let me start by stating an assumption: Phoenix is a great approach to building REST APIs. There are many reasons for this, but I will ass...
New
tmbb
This is a post to discuss the new Phoenix LiveView functionality. From Chris’s talk, it appears that they generate all HTML on the serve...
342 18243 126
New
AlexMcConnell
The reason that Rails is as popular as it is is because it’s very easy for relatively inexperienced developers to get a lot of work done....
588 19652 166
New
AstonJ
Are there any Elixir or Erlang libraries that help with this? I’ve been thinking how streaming services like twitch have exploded recentl...
New
100phlecs
Are there any downsides, like perf issues, to putting all functional components in CoreComponents as long as you prefix it with the conte...
New
acrolink
How does the two languages compare when it comes to server side application development? Any experiences or ideas? Thank you.
New
MarioFlach
Hello, I want to share a project I’ve been working on for a while: https://github.com/almightycouch/gitgud Background Some time ago I ...
New

Other popular topics Top

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
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 43487 311
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
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
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement