DEV Community

Discussion on: Write Better Code With [Functional Programming & Elixir]

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
elixirprogrammer profile image
Anthony Gonzalez

I guess you mean to remove the can_drink() function and rename legal() to can_drink() like the example below:

defmodule Person do
  def can_drink(age) when is_nil(age), do: "You're not a Person"
  def can_drink(age) when age < 18, do: "Nope!"
  def can_drink(age) when age < 21, do: "Not in the US!"
  def can_drink(age) when age >= 21, do: "YES!!!"
end

IO.puts Person.can_drink(age)
Enter fullscreen mode Exit fullscreen mode

You're right, but I thought that would've make more sense, more readable, and a little bit less confusing for people coming from other languages, the way I did it.