mmport80

mmport80

Let's talk about Guards

To those who consider themselves heavy users of guards, could you share your rules of thumb?

When do you or don’t you use guards?

How about errors resulting from guards?

How do you think about them for different types of functions?

First Post!

NobbZ

NobbZ

My rule of thumb is to use them when I need them. Or what do you mean?

And what do you mean by “errors from guards”? If something raises in a guard, it is considered as if the whole guard is false.

Most Liked

dimitarvp

dimitarvp

It is tedious alright. But I still try my best to use guards and limit what I receive even in my private functions.

Since Elixir is not statically typed and never will be – and since I want to use Elixir almost everywhere in my work – I compensate with guards and pattern-matching so as to catch errors as early as possible. Never again will I just catch the root of all exceptions (like many Java devs do) and just turn a blind eye to weak spots in the code.

BTW don’t forget about the relatively new defguard construct. It made my stricter code look cleaner.

StefanHoutzager

StefanHoutzager

You can use them as a complement to a patternmatch (edit: qqwy already named that, example:)

  defp get_task(%{:type => a} = element, socket) when a in ["exclusiveGateway","parallelGateway"],
    do:  get_tasks_after_element(element.id, socket)
Qqwy

Qqwy

TypeCheck Core Team

I use guards in two cases:

  1. To help with pattern-matching when I have multiple functions. Usually these functions are then ‘complete’ (as opposed to ‘partial’ functions).
  2. To constrain the input types of the functions, especially when they are part of the public API of whatever it is I am writing.

Last Post!

dimitarvp

dimitarvp

Absolutely. You are basically coding against a contract afterwards which makes things much clearer and more productive.

Yep. If we didn’t have that we might as well just code in vanilla Javascript. To me guards and pattern matching are a core value proposition of Elixir.

Where Next?

Popular in Discussions Top

AstonJ
Are there any Elixir or Erlang libraries that help with this? I’ve been thinking how streaming services like twitch have exploded recentl...
New
AstonJ
If a newbie asked you about Phoenix Contexts, how would you explain the basics to them? Feel free to be as concise or in-depth as you li...
New
ben-pr-p
In general I’ve been sticking to this community style guide GitHub - christopheradams/elixir_style_guide: A community driven style guide ...
New
pdgonzalez872
If this has been asked here before, please point me to where it was asked as I didn’t find it when I searched the forum. Maybe a mailing ...
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
thojanssens1
It would be nice to be able to define a redirect from one route to another from the router.ex file. E.g.: redirect "/", UserController, ...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New

Other popular topics Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36689 110
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

We're in Beta

About us Mission Statement