Elixir Cheatsheet: A Node.js Developer’s Transition Guide

6 months ago 2
Club.noww.in

We’ve previously written about the reasons for trying Elixir out, as well as a how-to-get-started guide. However, there is still a long way ahead of you after firing up your thrusters. While the tutorial and documentation of both Elixir and Phoenix are the best I’ve ever seen by a great margin, the world of BEAM and OTP will be quite alien to what you are used to as a JS/TS developer. To make the embracing of unfamiliar concepts easier, we’ve created a cheatsheet to serve as an anchor during your journey.

The basics: Variables, Data Types, and Immutability and More

We start with the basics: primitive types, lists, and maps. The basic building blocks of applications in any language. This section acts as a quick reference for those familiar with JavaScript but new to Elixir’s strange and exciting ways.

One of the first things you’ll notice is the difference in variable handling. Unlike JavaScript, variables in Elixir are immutable, meaning their values cannot be changed after assignment. This enforces a safer programming style and eliminates the risk of unintended side effects.

Elixir also uses atoms instead of symbols, and thanks to pattern matching, they often also replace booleans. These atoms are lightweight, immutable entities that are perfect for representing simple data like true/false or unique identifiers.

String manipulation should feel familiar, with interpolation supported using double quotes and character escapes similar to JavaScript. However, Elixir offers sigils for string creation, providing flexibility depending on your needs.

For data structures, Elixir provides lists and tuples, similar to arrays in JavaScript. Lists are implemented as linked lists, so they are great for storing variable length data when you might need to append (and not push!) new elements to the collection, while tuples have defined size and thus are used for fixed data. 

Maps, on the other hand, resemble JavaScript objects but allow any data type, not just strings, as keys. This flexibility makes them powerful tools for storing and organizing diverse data.

Beyond these fundamentals, Elixir offers keyword lists and the enumerable protocol. Keyword lists provide a concise way to associate data with keywords, while the Enum module offers powerful functions for common operations on enumerable data structures.

Control Flow

Elixir offers a familiar-looking if-else and case and with blocks for controlling the flow of your app. But following in the footsteps of Ruby, we also have,  unless, if we’d prefer not writing not or !. We also have an old friend you might recognize. Don’t be fooled though, as they are not what they seem! When comparing these keywords with JavaScript, mostly everything is a false friend due to Elixir’s expression-oriented nature, so you’ll need to come back to this section often in your first weeks. 

Functions

Elixir’s functions come in various flavors and attached goodies, offering flexibility and expressiveness in your code. In the cheatsheet, you can find examples for:

Anonymous Functions:

  • Defined with fn and end keywords.
  • Used for short, one-time operations.
  • Can be assigned to variables for later use.

Module Functions:

  • Defined within modules using def or defp (private) keyword.
  • Public by default, requiring explicit marking as private if needed.
  • Offer clear organization and separation of concerns.

Function Signatures:

  • Described using Module.function_name/arity, where arity is the number of arguments.
  • Can be overloaded based on argument number or type using pattern matching.

Overloading Functions:

  • Achieved through pattern matching, guards, or default arguments.
  • Allows defining multiple functions with the same name but different argument combinations.

Pipe Operator (|>)

  • Chains function calls together, automatically passing the previous output as the first argument to the next function.
  • Offers concise and readable function chaining.

Capture Operator (&)

  • Captures functions into anonymous functions.
  • Useful for creating anonymous functions from existing named functions or passing functions as arguments.

Remember, these are just some of the fundamentals of functions in Elixir. The language offers further features like recursion and higher-order functions for building complex and elegant solutions, which would be too much to capture in a cheatsheet.

Pattern matching

Pattern matching is probably the most powerful and convenient property of functional languages. A cheatsheet cannot do justice to it, but at least you can come back and see the related language constructs:

  • Match operator (=): Its usage for variable assignment, value comparison, and data deconstruction (maps, lists, structs, etc.).
  • Function invocation: How pattern matching is used to identify the correct function based on factors like module name, function name, arity, argument types, default arguments, and function guards.
  • case expressions: Utilizing pattern matching for conditional branching.
  • Pin operator (^): Preventing variable reassignment during pattern matching within functions.

Modules and Structs

ES Modules and Elixir modules are similar only in name, so we included a lengthy explanation of them in the cheatsheet. We tried to make order regarding alias, require, import and use, because those can be confusing at first. Hint: require has nothing to do with it’s JS counterpart. But you probably guessed it by now. 

You can also find examples for Structs. While Elixir is not a statically typed language like TypeScript, Structs can provide the necessary type definitions when needed. Used in tandem with pattern matching, you’ll soon realize that type safety is overrated, especially when you feel how efficient you can be when you can forgo type wrangling and gymnastics, that is so common when writing TS.

Ready to Dive In?

We stand by what we said earlier: learning Elixir start paying dividends quickly. However, the first steps can be daunting, as there are new concepts you need to get familar with, and the syntax can feel alien at first. But whether you’re exploring Elixir for a specific project or keen on expanding your programming repertoire, we hope this cheatsheet will prove useful when you make the leap from Node.js to Elixir.

The post Elixir Cheatsheet: A Node.js Developer’s Transition Guide appeared first on RisingStack Engineering.

Read Entire Article