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
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Other popular topics 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
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
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
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
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
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

We're in Beta

About us Mission Statement