docjazither

docjazither

Mix credo complains about having more than 9 case clauses

Hi OGs,
I have a function as follow:

defp comparing_statuses(duplicates) do
    statuses = count_duplicates_statuses(duplicates)

    case statuses do
      {"one", _, "zero", _, _, _} ->
        iterate_through_duplicates_with_status_and_remove(duplicates, "approved")

      {"one", _, "one", _, _, _} ->
        deleting_approached_and_approved_case(duplicates)

      {"two or more", _, "zero", _, _, _} ->
        accumulating_required_status(duplicates, "approved")

      {"zero", "one", _, _, _, _} ->
        iterate_through_duplicates_with_status_and_remove(duplicates, [
          "invalid",
          "under_investigation",
          "suspended"
        ])

      {"zero", "two or more", _, _, _, _} ->
        accumulating_required_status(duplicates, [
          "invalid",
          "under_investigation",
          "suspended"
        ])

      {"zero", "zero", "one", _, _, "zero"} ->
        iterate_through_duplicates_with_status_and_remove(duplicates, "approached")

      {"zero", "zero", "one", _, _, "one"} ->
        iterate_through_duplicates_with_status_and_update(
          duplicates,
          "approval_requested",
          "approved"
        )

      {"zero", "zero", "two or more", _, _, "two or more"} ->
        accumulating_required_status(duplicates, "approval_requested")

      {"zero", "zero", "two or more", _, _, "zero"} ->
        iterate_through_duplicates_with_status_and_remove(duplicates, "approached")

        handle_or_email_deletion_check(duplicates)

      {"zero", "zero", "zero", "one", _, _} ->
        iterate_through_duplicates_with_status_and_remove(duplicates, "rejected")

      {"zero", "zero", "zero", "two or more", _, _} ->
        accumulating_required_status(duplicates, "rejected")

      {"zero", "zero", "zero", "zero", "one", _} ->
        iterate_through_duplicates_with_status_and_remove(duplicates, "external")

      {"zero", "zero", "zero", "zero", "two or more", _} ->
        accumulating_required_status(duplicates, "external")

      {"zero", "zero", "zero", "zero", "zero", "two or more"} ->
        mass_deletion_check(duplicates)
    end
  end

And mix credo complains me that function is too complex (CC is 15, max is 9)

I can not break the switch case into two functions, as one function can throw an error stating that pattern is not recognised

Any ideas ?
Thank you

Marked As Solved

msimonborg

msimonborg

use multiple function heads

defp comparing_statuses(duplicates) do
  statuses = count_duplicates_statuses(duplicates)
  handle_statuses(statuses, duplicates)
end

defp handle_statuses(statuses, duplicates)

defp handle_statuses({"one", _, "zero", _, _, _}, duplicates),
  do: iterate_through_duplicates_with_status_and_remove(duplicates, "approved")

defp handle_statuses({"one", _, "one", _, _, _}, duplicates),
  do: deleting_approached_and_approved_case(duplicates)

  # etc.
end

Also Liked

bmitc

bmitc

I personally would just disable the Credo check, which I have done in other projects because of this. I find the multiple function clause approach too verbose in cases like this. It increases the footprint of the function with exactly duplicated information and text. In this case, the case returns are short and simple, and I would prefer case over the function clause approach here.

msimonborg

msimonborg

It’s a fair point. I read the question as “how do I re-write this”, not “how do I silence this”. Since the compiler treats the case and the multiple function clauses more or less the same way, to me this is the natural way to rewrite the code and make credo happy without touching config. What if the OP’s company requires that credo pass all checks and disabling is not allowed?

Personally, I do find the multiple function approach more readable, but it is all a matter of preference.

Where Next?

Popular in Questions Top

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
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
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
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
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
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New

We're in Beta

About us Mission Statement