TIL How to Select Merge with Ecto.Query How to Use Elixir's Compiler to Avoid Typos

10 Common Code Refactoring Experiences|Elixir

https://medium.com/blackode/elixir-code-refactoring-techniques-33589ac56231

Image

This article comprises of common Elixir coding techniques that you may or may not know.

A Jist of the Article

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")

However, you can also take this to the next level like

currency = price["currency"] || "USD"

ElixirWeekly: The Elixir Community Newsletter, covering community news you easily miss, shared on ElixirStatus and the web, in one email every Thursday.