Skip to content

Instantly share code, notes, and snippets.

@ktravers
Last active July 12, 2018 03:05
Show Gist options
  • Save ktravers/c09d29be8ccaa4617ccf1f53ab495424 to your computer and use it in GitHub Desktop.
Save ktravers/c09d29be8ccaa4617ccf1f53ab495424 to your computer and use it in GitHub Desktop.
Unwieldy nil checks in Elixir
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