Code Flow Summary
Congratulations! We covered a lot of ground together! If you took the time to do the exercises then you’ve built up excellent hands-on experience applying these new concepts.
Let’s briefly review what we’ve covered here and stress some take-away points.
What we covered
- The Pipe Operator
|>
does a simple job but it enables pipelines which are more elegant and readable. - A
case
statement supports guard clauses and they can be piped into. - A Keyword list is a list of tuples. It is frequently used for passing options to a function.
- The Railway Pattern adds pattern matching clauses and pipelines to create a “happy path” and “failure path”. Works well on functions defined in a module used as a workflow.
- The “with” statement uses pattern matching to define a “happy path”. It replaces nested
case
statements and works well for pulling functions together from different parts of the system. - Looping in Elixir is done using pattern matching and recursive function calls.
- Tail recursion is a feature of many functional languages that makes recursive calls able to “loop” forever without a stack overflow.
- The
Enum
module has functions that help with processing a list. They includeEnum.each/2
,Enum.map/2
, andEnum.reduce/3
. for
comprehensions provide a simple way to loop. It is a Swiss Army Knife feature that can do many things. A great tool to have on-hand.- Error handling is used less in Elixir than other languages. A
raise
needs arescue
. Athrow
needs acatch
and anexit
is a special type ofthrow
. - The
if
statement exists but is used far less frequently. - A nested
if
is an anti-pattern. - The
cond
statement replaces theif ... else if
structures from other languages.
Extra things we picked up on the way
IO.inspect/2
is a great debugging and insight tool. It is “pipe friendly” and can be added liberally. Using thelabel: "My Label"
option really helps to identify which inspect call we are looking at.- When defining an
alias
, we can use thealias MyApp.Thing, as: OtherName
option to override what it is called. - We talked about the Range type (ex:
1..10
) and how they can be used for looping. - Anonymous functions support multiple clauses. This gives added flexibility when working with
Enum
list processing functions. - Elixir has “truthy” and “falsy” evaluations. A “falsy” value is
nil
orfalse
. A “truthy” values is anything that is notnil
orfalse
.
Lessons learned
Elixir has immutable data, is a “functional language”, uses recursion, doesn’t have “objects”. This can feel very unfamiliar and frankly a little scary. It is exactly these characteristics that are the foundation for the powerful concurrency model and the resilience of Elixir systems. This requires you to learn to do old things in a new way. Even the way you control the flow of your code changes.
Having completed this course, you have experienced for yourself that “you can do this”.
Feeling comfortable with Elixir takes time. You know the techniques, you’ve used the control patterns, and you know when to use them.
Now it is up to you to continue with it. Look for opportunities to use these Code Flow patterns. Don’t be afraid to refactor your code from one pattern into another as you realize something else is a better fit. This is where you really build experience and skill.
You got here on your own. I know you can do this. We did cover a lot and you can return to review anything you need. I think the most important thing you have learned from this is that you know you can do this.
You got this.
Download reference resource
Now that you have completed the course, as a special “thank you”, I want you to have a ready, portable, handy reference as a resource of everything we covered together. This is a PDF download of the course information. It is indexed and searchable so you can easily jump around and find something you want to refer back to.
7 Comments
Comments are closed on this static version of the site.
Comments are closed
This is a static version of the site. Comments are not available.