Member-only story

Make a note of

3 Elixir Pro Boolean Hacks

Life is short, find hacks to code longer

Malreddy Ankanna

--

Photo by vipul uthaiah on Unsplash

Hello Elixirs !

I feel glad to share some Elixir Boolean type usage hacks out of my experience. Please excuse me if you already knew the hack but, it is nothing wrong to revise once more.

1. not not == true !! (Double Bang)

It is handy when a bounded value results to nil, true, false, and some_value.Assume that you have to return truthness of a boolean based on this bounded value.

Following are the examples of a value and it’s output.

value = nil      :: false
value = false :: false
value = true :: true
value = anything :: true

Before

staus = 
if not is_nil(value) && value do
true
else
false
end

Hack

status = !!value

We have completely replaced the if macro here.

Check out the execution screenshot below

Double Bang execution screenshot

--

--

Malreddy Ankanna
Malreddy Ankanna

Written by Malreddy Ankanna

Programmer & Writer, I write about coding, thoughts, ideas, personal musings, technical articles, and tutorials.https://bio.link/blackode

Responses (1)