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

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
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 I...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29603 241
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
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
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
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
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
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

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement