Guardians Of The Elixir Functions — Tentamen Software Testing Blog

Image by from

TL;DR

In the previous post, we introduced , which is basically applied pattern matching to function input arguments. As pattern matching could not include logical operators, guards are mechanisms that extend function overloading with logical operators. This post is part of the functional language series, and it is based on the remarkable book Elixir In Action by Sasa Juric.

As pattern matching in function arguments is powerful mechanics that make your code more concise and readable (remember to put overloaded functions next to each other!), we must reach for function guards when it comes to logical expressions.

As I think that Croatian VAT is too much for our poor economy, I would like to add an expensive country string if a country has a VAT greater than 20%.

Elixir Function Guards

As your Elixir experience will grow, you will become more ambitious. Let’s use functions in guard expression. As Elixir forces you to write clean and simple code, you will be surprised that it is impossible to use complex functions in guards. documentation is to date place to check what is allowed.

Remember

  • You reach for guards when you need an in-function argument pattern matching logical expressions.
  • Elixir is not the type-safe language; you could easily end up comparing an atom with a number without any warning.
  • In guards, you can use a limited set of functions.
  • Elixir function overloading and guards help you to avoid complex, long and nested if then else statements

Originally published at on February 25, 2021.

--

--

--

Founder of Tentamen, software testing agency.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Dealing with data in a micro-services world

Faster .NET development on Kubernetes with Skaffold

Laptop showing code on screen.

Shared drive vs. My Drive — Admin permissions

Accident Lawyer Sandy Utah

accident lawyer sandy utah

Importance of Hackathons in Students’ Career

Ultralearning Python & Pygame

Introduction to gRPC

Aiming for perfection: Can PerfectIt make you a better editor?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Karlo Smid

Karlo Smid

Founder of Tentamen, software testing agency.

More from Medium

Elixir and Ecto

An opinionated Elixir Style Guide #2

Ranked data with Ecto window Queries

Elixir’s best practices: Achieve secure authentication