mate
Is it normal to use "case" all the time?
Hello,
I’d like to kindly ask about using case vs. other statements/conditions.
I different languages (all OOP) I used if/else most of the time. I didn’t use case at all mainly because it needed break and I found if/elseif/elseif/elseif/else better readable.
I spent whole weekend on Elixir (great weekend! ;-)) and now I’m looking at my code and:
I used:
caseapprox 50 timesif/else2 times forif x in some_listandif x > 0and looking at it now I might replace it by multi-clause functioncond1 time for:
cond do
n > length -> :error
length == n -> {:ok, ""}
true -> {:ok, do_something_with(x)}
end
And everything else is case (not counting guard clauses/pattern matching in function arguments).
I even used case for stupid things like
case value do
[] -> :error
value -> {:ok, value}
end
Can anyone please tell me is this normal in Elixir or am I crazy / crazy love with case ?
P.S. this looks like funny joke question
but it’s actually real and serious questions.
Kind regards,
Mat
Most Liked
NobbZ
Yes, pattern matching is the favored way of doing conditionals in elixir, so it is absolutely normal to use much more case/2 and function head pattern matching than it is to use an if/2 (which at the end is a macro which expands to a case/2 anyway).
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









