Save Ukraine

Erlang: if or case of

Christian Kruse,

The last few weeks I started to learn Erlang, a functional programming language designed to be very solid. This is my first functional programming language I am really learning. At the university I had a small look at Haskell, but I was never really interested in it.

After writing my first few lines of code, the following question came to my mind: when do we use case of and when do we use if? In traditional non-functional programming languages the difference was clear. But the differences blur in functional languages. So what would you write, and why? Given the following example:

case is_pid(Pid) of
true ->
Pid ! {send, ["QUIT :", ?QUITMSG]},
Pid ! quit;

false ->



is_pid(Pid) ->
Pid ! {send, ["QUIT :", ?QUITMSG]},
Pid ! quit;

_ -> ok


Personally I would prefer the second variant, since one can see on the first look what which branch does. What do you think, and why?