Elixir Short Circuit Operators — Tentamen Software Testing Blog

Image for post
Image for post

TL;DR

Short Circuit Operators

We have two Short Circuit Operators, || and &&. We use them to chain several functions that return a boolean value.

False and True

||

Image for post
Image for post

You would use || short circuit operator in your application for function chaining:

email_login || facebook_login || google_login

In this example, your application provides three ways to log in, using registered email/password credentials, Facebook, or Google login. Let users in for any of those authentication providers.

&&

Image for post
Image for post

You would use and short circuit operator if the user has to satisfy several conditions to log in:

same_ip && authenticated? && is_admin?

Remember

  • && returns the last expression only if all previous expressions are true
  • when to use those short circuit operators

Originally published at https://blog.tentamen.eu on July 21, 2020.

Written by

Founder of Tentamen, software testing agency.

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