The Rigid Code Behind Smooth Code— Elixir Intermediate
Tags: elixir, macros, programming, functional
Published: 2018-08-03 12:57:28

---
title: The Rigid Code Behind Smooth Code— Elixir Intermediate
published: true
description: This article shows you the rigid lines of code behind smooth lines you write in elixir.
tags: [elixir, macros, programming, functional]
---


What you see is more simplified.

![Photo by [Jamie Street](https://unsplash.com/photos/d299Wu2xipY?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText) on [Unsplash](https://unsplash.com/?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)](https://cdn-images-1.medium.com/max/6158/1*wvfO4tFXIbDev34wNMu4yw.jpeg)*Photo by [Jamie Street](https://unsplash.com/photos/d299Wu2xipY?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText) on [Unsplash](https://unsplash.com/?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)*

This article shows you the rigid lines of code behind smooth lines you write in elixir.

As we know, the most of the Elixir programming language is written in Elixir itself with the power of Macros and also we all know why we love to code in Elixir because of its simplicity. But, today we are going to walk back to find the light in the dark.

I just want you to observe the following line of code for a while

```elixir
if 1+2==3, do: :this, else: :that
```

![](https://cdn-images-1.medium.com/max/2048/1*NPv3-LyItatGwa0DYAkjCw.png)

Here, I am not gonna ask you what this results to. But, I am asking you how this gonna transform eventually.

Let’s observe the transformations.

![](https://cdn-images-1.medium.com/max/2048/1*udm4XJbvIBVtuyZ_goqkJQ.png)

As we know the elixir allows the function call with out `()` . This is why the function calling is simple. But, I recommend to use `()` which is more understandable about what is going on.

![](https://cdn-images-1.medium.com/max/2048/1*ZcAq70QUQg2wpF5hMDIYwg.png)

If you observe above line, we have couple of `,` .

![](https://cdn-images-1.medium.com/max/2048/1*lJdd24JXmmO3J-PcVGtt6A.png)

This is more likely if is taking three parameters `1+2==3` , `do: :this` and `else: :that` but, in real it is taking **two**.

![](https://cdn-images-1.medium.com/max/2048/1*_qQGBo3-Yigd6qUAzvrXHg.png)

The last two parameters `do: :this` and `else: :that` are together called a KeyWord List .

If you don’t know what a keyWord List is, It is likely a list where its list elements should be tuples of two elements where first element should be an `atom` .

In one sentence, keyword list is a list of tuples where tuple size should be **two (2)** and first element in a tuple is an atom . In real it is **list** of **key-value **pairs where **key** is an atom and **value** can be any .

Consider the following example

```elixir
iex> list = [{:a, 1}, {:b, 2}]

[a: 1, b: 2]

iex> list == [a: 1, b: 2]

true
```

Here `[{:a, 1}, {:b, 2}] == [a: 1, b: 2]` . Enough with keyword lists .

Let’s jump back to our line of code

```elixir
if 1+2==3, do: :this, else: :that
```

If the last parameter to pass is a KeyWord List , we no need to wrap elements inside the `[]` square brackets likely we do in general.

`hello("medium", user_name: "blackode", lang: "elixir")`

is same as

`hello("medium", [user_name: "blackode", lang: "elixir"])`

Here, both are considered as valid.

So, the line of code is transformed from

```elixir
if 1+2==3, [do: :this, else: :that]
```

to

```elixir
if( 1+2==3, [do: :this, else: :that] )
```

![](https://cdn-images-1.medium.com/max/2048/1*VQ_qpOhvhl8g1-CLf2j9LA.png)

Now, observe the first parameter `1+2 == 3` which is `(1+2)==3`.

![](https://cdn-images-1.medium.com/max/2048/1*cHtg90yygSlTWu2K-ViEJg.png)

![](https://cdn-images-1.medium.com/max/2048/1*uv13Xus0Lve8FN3l2O5biA.png)

Here, before resolving `==` we have to add `(1+2)` .

The `(1+2)` is more likely a function with two parameters. It is an [infix](https://en.wikipedia.org/wiki/Infix_notation) notation and + is called an [infix](https://en.wikipedia.org/wiki/Infix_notation) operator.

It is a different way of doing certain functions where the **function_name** is in the **middle**. The **left **and **right **values are passed as **first **and **second** parameters respectively to the function.

So, the traditional syntax of representing (`1+2)` is `+(1,2)` .

![](https://cdn-images-1.medium.com/max/2048/1*am6xvLObA6laoG5Wj377ug.png)

Similarly we break down the another infix operator `==` as `==(+(1,2), 3)` just like we did for `+` nothing more fancy here.

Well, so far we have break down the `+(1,2)==3` to `==(+(1,2), 3)` .

![](https://cdn-images-1.medium.com/max/2048/1*cgOT6wr3V7HB-nauJOo91Q.png)

Here, we cannot use the infix operators with out its module name where they are defined. All the operators are defined inside the module called Kernel . This is the default module in elixir.

So, again we break down `==(+(1,2), 3)` to `Kernel.==(Kernel.+(1,2), 3)`

![](https://cdn-images-1.medium.com/max/2048/1*zzwpKayodY1FT-sBSYzisg.png)

Now, the line is transformed to as following

```elixir
if(Kernel.==(Kernel.+(1,2), 3), [do: :this, else: :that])
```

We all know that if is a macro which is more likely represented Kernel.if

So, putting all together and breaking down the infix functions, the line again is transformed into

```elixir
Kernel.if( Kernel.==(Kernel.+(1,2), 3), [do: :this, else: :that ] )
```

![](https://cdn-images-1.medium.com/max/2048/1*1ywFojdPwfzs99ryu7F5gA.png)

![live execution in iex](https://cdn-images-1.medium.com/max/3824/1*xaDH1bDxvhAVjBwZsMwcZw.png)*live execution in iex*

The best part is, the elixir compiler will take care of all these transformations.

We no need to write all the stuff like above.

Hope you got some idea about hotness behind the cool syntax. For deeper understanding, read more about the **[MACROS](http://elixir-lang.github.io/getting-started/meta/macros.html).**

Happy Long Coding Life :)