Functions starting with a conditional

This is a pattern I see a lot with people new to Elixir or pattern matching, especially with a good Ruby background where they already have a great practise of writing small functions.

The first thing to happen in these functions is we check something that was passed in.

defmodule Example do def is_zero?(number) do

if number == 0 do

true

else

false

end

end end

Contrived example for sure and first thing you could do is get rid of the useless if structure like this

defmodule Example do def is_zero?(number) do

number == 0

end end

But lets imagine this was little more complicated. What we can do instead is do the conditional branching using pattern matching in the function signature

defmodule Example do def is_zero?(0), do: true

def is_zero?(_), do: false end

This is particularly nice if you started with a 8–9 line function where both branches had 3 or so lines of code.

This is also handy for case statements

defmodule Example do def send(message) do

case message.type do

:sms ->

# some SMS send code

:email ->

# email send code

:slack ->

# Slack code

_ ->

# Bad type catch

end

end end

becomes something like

defmodule Example do def send(%{type: :sms}) do

# some SMS send code

end def send(%{type: :email}) do

# some email send code

end def send(%{type: :slack}) do

# Slack code

end def send(_) do

# Bad type catch

end end

or even