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

Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New
pillaiindu
In django there is a cache framework backed by memcached. Rails also puts a lot of emphasis on caching, and even the idea of russian-doll...
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
pillaiindu
I want to convert a Phoenix LiveView CRUD website to a CRUD mobile app. What do you think is the easiest way to do so?
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
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
RudManusachi
What configs will make sense to put to runtime.exs? – A bit of how I configure apps: I have generic configs in config/config.exs, dev...
New
AstonJ
Can you believe the first professionally published Elixir book was published just 8 years ago? Since then I think we’ve seen more books f...
New
scouten
I’m looking for a host for the server part of a small (personal) side project that I’m working on. It’s currently written in Node.js and ...
New
AstonJ
It’s been a while since we’ve had a thread like this, so what better way to kick off the year with :003: What does being an Elixir user ...
New

Other popular topics Top

Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
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
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement