We all love to cheat right? chill 😅, I mean using cheats to make our code simpler yet cooler.

Here are some elixir cheats that I hope can help you code more effectively. I’ll continuously update this blog on my website (Elixir: Cheatsheet) every time I get to learn new techniques in elixir, I will also appreciate it if you have some corrections or other cheats that I can add here. Thanks!

UndefinedFunctionError
function nil.value/0 is undefined

Have you experienced this error? especially when trying to get a value 3 structs down to the main variable?ex: @music.singer.name, you can try this solution to never ever encounter this again.

def get(map, fields) when is_list(fields),
do: Enum.reduce(fields, map, fn field, map -> if map, do: Map.get(map, field) end)
def get(map, fields) when is_list(fields),
do: Kernel.get_in(map, fields)
reference: Kernel.get_in/2
<%= get(@music, ~w(singer name)a) %>
This will never return an UndefinedFunctionError
data 
|> Enum.map(fn f -> %{name: f.name} end)
data
|> Enum.filter(fn f -> f.status == "active" end)
data
|> Enum.find(fn f -> f.id == id end)

I’m sure you’ve tried using one of these code blocks and like me, I know you think that it’s kinda boring.

Not only that it became shorter removing fn f -> ... end , it became cooler to look at too.

data 
|> Enum.map(& %{name: &1.name } end)
data
|> Enum.filter(& &1.status == "active")
data
|> Enum.find(& &1.id == sample_id)
Cool right ??? 😅
data = %{active: true}if data.active do
"Active"
else
"Not Active"
end
# Shorthand
if data.active, do: "Active", else: "Not Active"

Shorthand conditions depend on what you are doing inside your condition, if you’re doing a one-liner code, better use shorthand, but if not I suggest to do it the old fashioned way.

Instead of doing:def is_active() do
...
end
You can try doing: def is_active(), do: ...
data = %{active: true}def is_active(data) do
if data.active do
"Active"
else
"Not Active"
end
end
# The code above is not safe, if data is nil it will return an UndefinedFunctionError, You can use pattern matching with shorthand method:def is_active(%{active: true}), do: "Active"
def is_active(_), do: "Not Active"
# Whats great here is that, no matter what type of value you pass on the method(list, nil or an atom), as long as it does not match the first method, it'll always go to the default method catcher, returning "Not Active".

I think this is the coolest of them all, and personally, it made my code a little safer. But in case you already know this just skip.

data = %{name: "alvin"}# This is a guard
def cool_method(data) when not is_nil(data), do: ...
def cool_method(_), do: ...
or # This is a pattern match
def cool_method(%{name: "alvin"}), do: ...
def cool_method(data), do: ...
# Combination of both
def cool_method(%{name: name} = data) when not is_nil(name), do: ...
def cool_method(_), do: ...

I can talk all day about guards and pattern matching but you’ll get bored so instead, you can check more here: guards and pattern match

Check out the full story on my website here: Elixir: Cheatsheet