Cleaner alternative for nested case/cond/if statements?

I’m trying to find a cleaner coding style for what I’ve got happening repeatedly in my code right now. This is looking very pyramid of doom-ish

# Variables in scope
%{ var1: var1, var2: var2, var3: var3}
case function1(var1) do
    # Success case one
    {:ok, %{pattern1: _}} ->
      case function2(var2) do
        {:ok, %{pattern2: _}} ->
          # Etc with the nesting as need be
          Logger.info()

        other ->
          Logger.error("Oops")
      end

    # Success case two
    {:ok, %{pattern3: _}} ->
      case function3(var2) do
        {:ok, %{pattern4: _}} ->
          # Etc with the nesting as need be
          Logger.info()

        other ->
          Logger.error("Oops")
      end

    # Failure case
    other ->
      Logger.error("He's dead, Jim. Cause of death: #{inspect(other)}")
  end

I know that as an alternative I could put the pattern matching/fail catching in a series of functions but this isn’t necessarily any more readable as the functions should be grouped by name not by logical association. Once the logic gets more complicated, it is actually worse imo for readability.

Is there some alternative I’m missing?

https://hexdocs.pm/elixir/Kernel.SpecialForms.html#with/1

Have you tried the with macro?

You have a tree of cases here (not one happy path, different leaves between both first level successes), so unless you move stuff into functions (anonymous or named) you’d probably need to stick with what you have.

Yes. I didn’t actually realize until it was pointed out to me that with with you can pass the results from one call into the next one. The catch is that the errors become more grouped together which in some cases is beneficial. It seems like with is probably the happy medium here.

Thanks for the suggestion!

Just for future searches, would you mind including the with version so we have a before and after?

3 Likes

Here’s a rough translation although not direct as in the original I’ve got two success cases where only the first one will match whereas in the translation both with (potentially) match. This is the general idea though:

%{var1: var1, var2: var2, var3: var3}

    with {:ok, %{pattern1: _}} <- function1(var1),
          # Now you can use pattern1
         {:ok, %{pattern2: _}} <- function2(%{pattern1: pattern1, var2: var2}) do
      # Etc with the nesting as need be
      Logger.info()
    else
      # Failure case
      other ->
        Logger.error("He's dead, Jim. Cause of death: #{inspect(other)}")
    end

    with {:ok, %{pattern3: _}} <- function1(var1),
         {:ok, %{pattern4: _}} <- function3(var2) do
      # Etc with the nesting as need be
      Logger.info()
    else
      # Failure case
      other ->
        Logger.error("He's dead, Jim. Cause of death: #{inspect(other)}")
    end
2 Likes