Member-only story
Make a note of
3 Elixir Pro Boolean Hacks
Life is short, find hacks to code longer
4 min readAug 30, 2020
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