Papillon6814

Papillon6814

Handling nil in case

Hello. I’m thinking the way to handle nil in case.

|> case do
    x when not is_nil(x) -> 
      ...
      {:ok, "xxxxxx"}
    nil ->
      ...
      {:error, "xxxxxxxx"}
end
|> case do
    nil -> 
      ...
      {:error, "xxxxxxxx"}
    topic ->
      ...
      {:ok, "xxxxxxxx"}
end

When I handle nil in case, I write codes in a latter way. What do you think of that?

Most Liked

dimitarvp

dimitarvp

In case, I always go from most concrete to least concrete, like so:

case something do
  nil -> "nil"
  :a -> "the atom :a"
  text when is_binary(text) -> "text"
  list when is_list(list) -> "list"
  other -> raise(ArgumentError, "unexpected value: #{inspect(other)}"
end
kokolegorille

kokolegorille

You can also do like this, if topic is some kind of struct.

|> case do
  %Topic{} = topic -> ...
  nil -> ...
end
dmitrykleymenov

dmitrykleymenov

About piping into case a good mention was in Good and Bad Elixir post. May be you should consider to match it to some variable? Especially with nil result possibility, you could rewrite it to if some_result, do: something, else: another

Last Post!

dmitrykleymenov

dmitrykleymenov

At least that is said in the article :slight_smile: If you think it makes sence, yeah, you may assign intermediate result into variable and use it in case explicit. But it’s hard to tell without upstream code.

Where Next?

Popular in Questions Top

Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
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
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
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
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

Other popular topics Top

New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
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
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New

We're in Beta

About us Mission Statement