Quick & Easy Elixir Refactorings — Part 4

Nested conditionals

Ville
HackerNoon.com
Published in
3 min readDec 2, 2017

--

In the previous part we looked at how to refactor away conditionals in the middle of your functions by passing in a function instead. Before that we looked at refactoring away conditionals at the beginning and end of your functions.

If you’ve not read through the series yet I recommend you start from the beginning

Part 1 — Functions starting with a conditional
Part 2 — Functions ending with a conditional

Part 3 — Conditionals in the middle

In this post we will look at a way to deal with nested conditionals. One could be forgiven for thinking I don’t like conditionals by this point 😉

Nested conditionals

Nested conditionals are a far bigger problem in other languages and usually tend to be caused by having functions that are just too long and should already be split up but there are times when these come up and it’d be nice to have a concise way of dealing with them.

An example could be something like this

defmodule Example do  def authorised?(user) do
case UserAuthenticator.authorised?(user) do
{:ok, _user} ->
true
{:error, _user} ->
case LegacyUserAuthenticator.authorised?(user) do
{:ok, _user} ->
true
{:error, _user} ->
false
end
end
end
end

Not terribly elegant piece of code and slightly tricky to read as well. Imagine if instead of returning simple boolean we’d execute some longer piece of logic instead 😓

We could split up the authorised?/1 function to get rid of the nesting by doing something like this

def authorised?(user) do
case UserAuthenticator.authorised?(user) do
{:ok, _user} ->
true
{:error, _} ->
legacy_authorized?(user)
end
end
defp legacy_authorized?(user) do
case LegacyAuthenticator.authorised?(user) do
{:ok, _user} ->
true
{:error, _} ->
false
end
end

Hooray no more nesting 🙌 And by using the techniques from earlier on in this series we could eliminate the conditionals altogether to get something like this

def authorised?(user) do
user
|> UserAuthenticator.authorised?()
|> check_authorised()
end
def check_authorised({:ok, _}), do: true
def check_authorised({:error, user}) do
user
|> LegacyAuthenticator.authorised?()
|> check_legacy()
end
def check_legacy({:ok, _}), do: true
def check_legacy(_), do: false

This is nice when you have nesting in both branches of the initial conditional as it allows you to split all of that up across lot of little functions.

Fortunately with our example we only have a nested conditional in one of the branches which allows us to look at an alternative using the the with statement

def authorised?(user) do
with {:error, _} <- UserAuthenticator.authorised?(user),
{:error, _} <- LegacyAuthenticator.authorised?(user) do
false
else
{:ok, _} -> true
end
end

😮 😍

What we’ve ended up with is technically still a conditional but compared to our alternatives above I think it’s a very elegant solution to the initial problem.

If you’re not familiar with the with statement I urge you to play around with it as it can make code that is expected to follow a certain happy path really easy to reason about and certainly clearer than bunch of nested conditionals.

Please click the applause icon 👏 if you liked this post and follow me here or on Twitter @efexen to find short actionable posts where we’ll look at more simple tricks for leveling up your Elixir code 👍

--

--

Ville
HackerNoon.com

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