wmnnd

wmnnd

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?

Showing Posts 1 to 7

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.

AstonJ

AstonJ

I prefer the case as well, it’s much more natural (in the sense that even without knowing programming you would more likely be able to make sense of it) :smiley:

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.

cmo

cmo

Yes, if there is no else I use with but a single with/else should be a case in my mind.

pdgonzalez872

pdgonzalez872

I’ve seen matching in the success case work fairly well. So, your:

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

would become:

with {:ok, yay} <- something(),
     {:ok, _} = success <- this_is_the_happy_path(yay) do
    # do something here, usually log, maybe increment metrics
    success
else
  {:error, reason} -> handle_error(reason)
end
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.

— All posts loaded —

Where Next? Top

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 92995 915
New
caslu
I want to open this thread for you all to discuss and help those who really like Ash but are still hesitant to use it in a real project. ...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
GES233
I’m posting this in response to Jose’s recent tweet (Cr. link) : People are sleeping on Elixir for a coding harness: Hot-code swappi...
New
marciol
It would be helpful to have a list of companies worldwide that hire engineers without prior experience in Elixir. Often, it can be quite ...
New
durvia
Anyone running long-lived stateful processes on BEAM? We’re building an AI agent runtime and would love to compare notes. We’re a small ...
New
AstonJ
This might be a bit disturbing for some but it’s happening - computers running on living human neurons. They’ve made them smart enough t...
New

Other Trending Topics Top

marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews