Last active
July 12, 2018 03:05
-
-
Save ktravers/c09d29be8ccaa4617ccf1f53ab495424 to your computer and use it in GitHub Desktop.
Unwieldy nil checks in Elixir
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Account do | |
# Unwieldy nil checks | |
def display_name(%{first: nil, last: nil, username: nil}) do | |
display_name(%{}) | |
end | |
def display_name(%{first: nil, last: nil, username: username}) do | |
display_name(%{username: username}) | |
end | |
def display_name(%{first: nil, last: nil}), do: display_name(%{}) | |
# Happy paths | |
def display_name(%{first: first, last: last}) do | |
do_trim("#{first} #{last}") | |
end | |
def display_name(%{username: username}), do: "#{username}" | |
def display_name(_), do: "New User" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment