bmitc

bmitc

Having trouble with `formatter` and `case`

Hello! I am having issues with the Elixir formatter and a long case expression. What’s happening is that this:

defmodule Test do
  def evaluate_contract(%{from: from, to: to, value: value} = t, contract) do
    case contract do
      x when is_number(x) -> x
      s when is_binary(s) -> s
      [] -> true
      {} -> true
      true -> true
      false -> false
      {:if, co, tr, fa} -> if evaluate_contract(t, co), do: evaluate_contract(t, tr), else: evaluate_contract(t, fa)
      {:+, left, right} -> evaluate_contract(t, left) + evaluate_contract(t, right)
      {:*, left, right} -> evaluate_contract(t, left) * evaluate_contract(t, right)
      {:-, left, right} -> evaluate_contract(t, left) - evaluate_contract(t, right)
      {:==, left, right} -> evaluate_contract(t, left) == evaluate_contract(t, right)
      {:>, left, right} -> evaluate_contract(t, left) > evaluate_contract(t, right)
      {:<, left, right} -> evaluate_contract(t, left) < evaluate_contract(t, right)
      {:and, left, right} -> evaluate_contract(t, left) and evaluate_contract(t, right)
      {:or, left, right} -> evaluate_contract(t, left) or evaluate_contract(t, right)
      :from -> from
      :to -> to
      :value -> value
      _ -> false
    end
  end
end

gets formatted to:

defmodule Test do
  def evaluate_contract(%{from: from, to: to, value: value} = t, contract) do
    case contract do
      x when is_number(x) ->
        x

      s when is_binary(s) ->
        s

      [] ->
        true

      {} ->
        true

      true ->
        true

      false ->
        false

      {:if, co, tr, fa} ->
        if evaluate_contract(t, co), do: evaluate_contract(t, tr), else: evaluate_contract(t, fa)

      {:+, left, right} ->
        evaluate_contract(t, left) + evaluate_contract(t, right)

      {:*, left, right} ->
        evaluate_contract(t, left) * evaluate_contract(t, right)

      {:-, left, right} ->
        evaluate_contract(t, left) - evaluate_contract(t, right)

      {:==, left, right} ->
        evaluate_contract(t, left) == evaluate_contract(t, right)

      {:>, left, right} ->
        evaluate_contract(t, left) > evaluate_contract(t, right)

      {:<, left, right} ->
        evaluate_contract(t, left) < evaluate_contract(t, right)

      {:and, left, right} ->
        evaluate_contract(t, left) and evaluate_contract(t, right)

      {:or, left, right} ->
        evaluate_contract(t, left) or evaluate_contract(t, right)

      :from ->
        from

      :to ->
        to

      :value ->
        value

      _ ->
        false
    end
  end
end

You can try this with the online formatter. I think this is a pretty drastic format and greatly reduces the function’s readability. It also triples the case expression’s body.

I expected the if expression to get expanded to multiple lines but not have every other case expanded to multiple lines. I think after expanding the if, the formatter could put spaces between the clauses (I wouldn’t), but I don’t see the reason why it puts all the short match results on a new line just because of one long result expression.

Is this expected? If so, does anyone know how to configure or any existing custom plugins that work a little better for case? One option I have is to simply shorten the function name. Although not an ideal change due to formatting, that may be the easiest solution.

Most Liked

03juan

03juan

This is expected behaviour to keep formatting standard and predictable. If one clause is multi line all clauses are made multi line.

Breaking out the long lines into separate functions to keep the case clauses short is the right way. It’ll also make the code more understandable by using descriptive names.

elixir_style_guide#multiline-case-clauses

bmitc

bmitc

Yea, I am in one of the stages of “formatter grief” at the moment. :laughing: Although, I do wish there was some configuration for these opinionated stances for individual orgs, teams, or projects to settle on.

For now, I’m trying to move into the acceptance stage of formatter grief. I’m also building up so-called tricks that help me strike a balance between what I’d like vs what the formatter does. I’ll paste what I came up with tomorrow, and I think it’s a fine enough compromise.

I’ll take a look at the Elm docs you sent tomorrow. Thanks for those.

stefanchrobot

stefanchrobot

I think a better word here - and what @03juan actually had in mind - would be “consistency”. Granted, it’s consistency with it’s own rules which can be changed. But my take is that the fewer the rules the better.

Yes, this is the way. There are situations where no code formatter rules will make the code look good (“for most people :tm:”) unless you refactor it. For me, your code is a good example of that. Shortening the function name is one of the approaches (you can have top level evaluate_contract and then eval). Refactoring the common parts would be another:

{op, left, right} when op in @binary_ops -> evaluate_binary_op(t, left, right, op)

I’d say this is all or nothing. You need to be quite opinionated when deciding which options should be configurable.

Where Next?

Popular in Questions Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New

Other popular topics Top

lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31142 143
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52341 488
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
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
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement