The Definitive Guide on
10 Common Code Refactoring Experiences|Elixir
Sometimes things go wrong to show right path
Published in
10 min readAug 23, 2019
A better coding style for better results
This article comprises of common Elixir coding techniques that you may or may not know.
1— Default Value for absent key in a Map
We usually encounter a situation like we expect a map has certain key and if not we need to send some default value.
Immediately, we will end up using Map.has_key?
like in the following way
currency =
if(Map.has_key? price, "currency") do
price["currency"]
else
"USD"
If you see any such lines in your code then it is time to refactor them as
currency = Map.get(price, "currency", "USD")
If price
has key currency
with value nil
then it returns nil
. It just looks for the absence of a key to return default value.