Write Better Code 
With [Functional Programming & Elixir]

Write Better Code With [Functional Programming & Elixir]

The Beauty Of Functional Programming With Elixir

Writing code can get messy, especially when you are a beginner, sometimes you focus so much on making the code work that you forget how hard to read and understand it is, you might start using lots of if-else statements and make pretty long and confusing functions that do multiple things, making it difficult to maintain.

Let me tell you a little secret that senior developers don't tell you, they practice functional programming paradigms, functional programming may not be too popular, but learning those concepts can make you a better developer. Let's dive in with a simple example using the Elixir programming language.

defmodule Person do
    def can_drink(age) do
        if age != nil do
            cond do
                age < 18 ->
                    "Nope!"
                age < 21 ->
                    "Not in the US!"
                true ->
                "YES!!!"
            end
        else
            "You're not a Person!"
        end
    end
end

IO.puts Person.can_drink(age)

In that example, the module Person will check the age that is passed into the can_drink(age) function for legal drinking age, and return a string, conditionals in elixir are a simpler way to create if-else statements but we can make it way better.

defmodule Person do
    def can_drink(age) do
        if age != nil do
            cond do
                age < 18 ->
                    "Nope!"
                age < 21 ->
                    "Not in the US!"
                true ->
                "YES!!!"
            end
        else
            "You're not a Person!"
        end
    end

    def can_drink_better(age) do
        legal(age)
    end
end

IO.puts Person.can_drink(age)

We added the can_drink_better(age) function which calls a legal function, so now let's create the legal function.

defmodule Person do
    def can_drink(age) do
        if age != nil do
            cond do
                age < 18 ->
                    "Nope!"
                age < 21 ->
                    "Not in the US!"
                true ->
                "YES!!!"
            end
        else
            "You're not a Person!"
        end
    end

    def can_drink_better(age) do
        legal(age)
    end

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

IO.puts Person.can_drink_better(age)

We created 4 legal(age) functions in the short inline form, the first checks if age is nil, it has to be the first function that it gets called, in elixir functions are called from top to bottom, because if it's placed at the bottom when age >= 21 will always be true due that nil it's not an integer.

It might look a little bit confusing if you come from another language, in Elixir you can call a function with the same name multiple times once until all the conditions are met, that's the beauty of functional programming. So now we can just remove the conditional function and be amazed by the awesome result.

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)

Now our code looks cleaner than a baby's mind, functional programming with Elixir is an exquisite dish served in a 5 stars international restaurant.

   

   

Join The Elixir Army