Nested conditionals -------------------  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](https://hackernoon.com/tagged/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](https://medium.com/@efexen/quick-easy-elixir-refactorings-part-1-17376e9c455a)[ Part 2 — Functions ending with a conditional](https://medium.com/@efexen/quick-easy-elixir-refactorings-part-2-4cd66bad1b20) [Part 3 — Conditionals in the middle](https://medium.com/@efexen/quick-easy-elixir-refactorings-part-3-def1aae4b072) 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](https://twitter.com/efexen) to find short actionable posts where we’ll look at more simple tricks for leveling up your [Elixir](https://hackernoon.com/tagged/elixir) code 👍