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

jesse
Hi everyone, I hesitated to post this here because I don’t want you to think I’m spamming, but I’ve been working on a Platform-as-a-Serv...
New
New
AstonJ
If so I (and hopefully others!) might have some tips for you :slight_smile: But first, please say which area you’re finding most challen...
New
slashdotdash
Phoenix Live View is now publicly available on GitHub. Here’s Chris McCord’s tweet announcing making it public.
New
eteeselink
Hi all, In the last days, two things happened: A blog post titled “They might never tell you it’s broken” made the rounds. It’s about ...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
shishini
I think this twitter post and youtube video didn’t get as much attention as I hoped I am still new to Elixir, so can’t really judge ...
New

Other popular topics Top

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40082 209
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement