wmnnd

wmnnd

Readability of single-clause with statements

Apparently Credo considers single-clause with statements with an else branch a readability issue. I’m curious, what’s everybody’s opinion on this?

I think with can actually help readability when implementing a function with a “happy path”:

with {:ok, yay} <- something() do
  this_is_the_happy_path(yay)
else
  {:error, reason} -> handle_error(reason)
end

The alternative is, of course, to use good ol’ case:

case something() do
  {:ok, yay} ->
    this_is_the_happy_path(yay)

  {:error, reason} ->
    handle_error(reason)
end

Which option do you find more readable?

Most Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Personal $0.02: For single clauses the case is 100% better because the patterns all line line up and stand out better.

stevensonmt

stevensonmt

I would say more like 90% better just because I can appreciate a possible style guide that mandates with statements for consistent error handling. So that way you can easily identify functions that can return an error versus those that just return values that trigger conditional flow.

tomekowal

tomekowal

Funnily enough. The same topic popped in recently when we were upgrading a quite old credo version.
We decided to get rid of the rule.

For me, it depends.

We have a bunch of modules where all the functions are pipelines and only one of them had a single case:

defmodule A do
  def a do
    with {:ok, a} <- do_a(),
      {:ok, b} <- do_b() do
      {:ok, b}
    end
  end

  def b do
    with {:ok, c} <- do_c(),
      {:ok, d} <- do_d() do
      {:ok, d}
    end
  end

  def c do
    with {:ok, e} <- do_e() do
      {:ok, e}
    end
  end
end

The rule that a single-case with statement should always be rewritten to case did not make much sense in that scenario. Other than consistency, I always pitch for using the simplest tool for the job. Case is simpler than with.

Last Post!

D4no0

D4no0

I agree with your case, I would also change credo rules. In my opinion credo is a universal tool, for beginners they should follow guidelines from default config, for advanced users they should tailor the rules for their needs. Credo saved me countless times when I was working with newbies, instead of me personally telling them when to use if or case or when, credo would guide them into a decent style, from there I could explain them the things that credo didn’t cover.

Where Next?

Popular in Discussions Top

PragTob
Hey everyone, this has been on my mind for some time and I’d love your input on it! TLDR: I feel like maps are superioer for storing and...
New
New
rms.mrcs
A couple of days ago I was discussing with a friend about different approaches to write microservices. He said that if he was going to w...
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
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
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
mmmrrr
Just saw that dhh announced https://hotwire.dev/ Is it just me or is this essentially live view? :smiley: Although I like the “iFrame-e...
New

Other popular topics Top

baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40165 209
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44778 311
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
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

We're in Beta

About us Mission Statement