siddhant3030

siddhant3030

Function body is nested too deep

I have a case statement that is failing in credo. Giving me this error

“Function body is nested too deep” I know we can use “with” here but I’m failing to use it correctly

  case NaiveDateTime.from_iso8601(valid_time) do
      {:ok, datetime} ->
        case Somefunction(which give me true or false) do
          true -> true
          false -> false
        end

      {:error, _} ->
        "datetime is not valid"
    end

Guide me

Marked As Solved

lucaong

lucaong

As you are mapping the return value of the function to itself, you can just avoid the inner case:


case NaiveDateTime.from_iso8601(valid_time) do
  {:ok, datetime} ->
    Somefunction(which give me true or false)

  {:error, _} ->
    "datetime is not valid"
end

In this case, I don’t think that with would help (but it might, depending on the code outside the outer case statement, which we don’t see).

Also Liked

mudasobwa

mudasobwa

Creator of Cure

In this particular case, @lucaong already provided the best suggestion. In general, we usually introduce a private function with several clauses in such a case.

valid_time
|> NaiveDateTime.from_iso8601()
|> do_validate_instead_of_case()

[...]

defp do_validate_instead_of_case({:ok, datetime}) do
  case Somefunction(which give me true or false) do
    ...
  end
end

defp do_validate_instead_of_case({:error, _}),
  do: "datetime is not valid"
Sebb

Sebb

learn to use with correctly

siddhant3030

siddhant3030

Thanks :).

Last Post!

siddhant3030

siddhant3030

Thanks :).

Where Next?

Popular in Questions Top

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
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
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New

Other popular topics Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
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
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
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

We're in Beta

About us Mission Statement