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 :).

Where Next?

Popular in Questions Top

_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
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
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
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
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
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
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
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
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

Other popular topics Top

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
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
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
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
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52341 488
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
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New

We're in Beta

About us Mission Statement