Quick & Easy Elixir Refactorings — Part 1

Functions starting with a conditional

defmodule Example do  def is_zero?(number) do
if number == 0 do
true
else
false
end
end
end
defmodule Example do  def is_zero?(number) do
number == 0
end
end
defmodule Example do  def is_zero?(0), do: true
def is_zero?(_), do: false
end
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
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
defmodule Example do  def send(%{type: :sms}), do: # some SMS send code
def send(%{type: :email}), do: # some email send code
def send(%{type: :slack}), do: # Slack code
def send(_), do: # Bad type catch
end

--

--

--

Freelance Developer • Founder of @CodeHalf • Rubyist • Elixir Alchemist • Wannabe Data Scientist • Dad

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ville

Ville

Freelance Developer • Founder of @CodeHalf • Rubyist • Elixir Alchemist • Wannabe Data Scientist • Dad

More from Medium

Elixir — A Tincture for Functional Programming Part 1.5 Recursion

Ouroboros

What is a Bug?

Fundamentals of functional programming

First steps with Elixir (again)