mate

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:

  • case approx 50 times
  • if/else 2 times for if x in some_list and if x > 0 and looking at it now I might replace it by multi-clause function
  • cond 1 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 :wink: but it’s actually real and serious questions.

Kind regards,

Mat

Most Liked

NobbZ

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

Where Next?

Popular in Questions Top

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
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
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
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
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
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
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
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
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
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

We're in Beta

About us Mission Statement