How To Use Variables In Elixir

Karlo Smid
3 min readApr 19, 2020

Reading Time: 2 minutes

TL;DR

The essential ingredient of every programming language is variable. This post is part of the functional language series.

Elixir is a dynamic language, the same as Ruby and Python. Remember, what should every programming language support. The dynamic programming language will figure out the automatically variable data type. And Elixir has supporting functions for checking variable type.

Here is the first rule that you need to remember by heart:

A variable name should start with a lower letter or underscore.

Otherwise, you will get a cryptic error like this:

This error is horrifying, as it does not explains the root cause. This is a weak side of Elixir, cryptic errors. For now, we will accuse Elixir of templates of that.

One helpful trick from Ruby’s world is where variables could end with ? and !. It is good practice to use ? for binary variables. For example, is_croatian? the variable name gives you a hint that holds true/false value.

When you assign value to a variable, in Elixir, this is called binding. The variable name abba is a reference to the memory location where 10 is stored. You can rebind variable to refer to a new memory location with abba = 20. Every functional programming language has an immutable property. In the variable binding case, that means you can not overwrite memory location with 10. You get a new memory location for 20, and abba now references memory location with 20. When the memory location does not have any reference, it is ready for garbage collection. Yes, same as when you take out your garbage, that beer can is out of your apartment fridge, and that place in the refrigerator is ready for new beer can.

Remember

You learned a lot in this lesson:

  • Dynamic Language
  • Naming Convention
  • Garbage Collected
  • Variable Binding
  • Rebinding
  • Immutable

It is time to take out your rubber duck to try to explain to it all those topics. This is how you learn.

Homework

Bind to the variable named vat, your country VAT amount for the basic Rubik cube. What is the vat data type?

Originally published at https://blog.tentamen.eu on April 19, 2020.

--

--