# What did you first think of the Erlang syntax

**URL:** <https://erlangforums.com/t/what-did-you-first-think-of-the-erlang-syntax/365>\
**Category:** Chat / Discussions\
**Tags:** community, syntax\
**Created:** [October 29, 2021, 4:27am UTC](https://erlangforums.com/t/what-did-you-first-think-of-the-erlang-syntax/365 "2021-10-29T04:27:08Z")\
**Posts on this page:** 1\
**Showing post:** 22

<div class="post-metadata">

**Author:** ![bjorng](https://erlangforums.com/user_avatar/erlangforums.com/bjorng/32/174_2.png) [@bjorng](https://erlangforums.com/u/bjorng)\
**Post date:** [November 5, 2021, 12:23pm UTC](https://erlangforums.com/t/what-did-you-first-think-of-the-erlang-syntax/365/22 "2021-11-05T12:23:40Z")

</div>

> [@Maria-12648430](#):
>
> Don’t forget to mention `and`/`or` vs `andalso`/`orelse`. Not an inconsistency, but I think it is something that may raise an eyebrow or two for the newcomers.

`and` and `or` are the oldest ones, perhaps already present in OTP R1.

> [@Maria-12648430](#):
>
> Why on earth are there _two_ of each? To a newcomer, it might at first appear that they are really equivalent, that you can either use `and` /`or` but have to parenthesize the arguments, or use the unwieldy `andalso`/`orelse` (which take a while to “stick”, too) and omit the parentheses.
> 
> Later they learn that `andalso`/`orelse` are short-circuiting and `and`/`or` are _not_, and may even realize that that has some consequences when using them to end a recursion loop. But who on earth even needs non-short-circuiting operators, why do they even exist

That is probably why [Richard Carlsson](https://github.com/richcarl) and the HiPE group suggested that Erlang needed `andalso`/`orelse`. I don’t remember in which release we added them. Obviously we could not remove `and` and `or`.

In addition to those more obvious boolean operators, there are also two language constructs that are allowed at the top-level of guards. One is `,` which is an AND operation (in a guard, it is not noticeable whether it is short-circuiting or not):

```erlang
foo(A) when is_integer(A), A > 0 ->
    all_is_good.

```

Probably less well-known is `;`, which behaves similar to `orelse`:

```erlang
bar(A) when is_integer(A), A > 0; is_atom(A) ->
    all_is_good.

```

This is equivalent to:

```erlang
bar(A) when is_integer(A), A > 0 ->
    all_is_good;
bar(A) when is_atom(A) ->
    all_is_good.

```

My own rules for which operators/functions to use are the following:

- Never used `and`. (I have yet to come up with a real-world use case where I would need a strict AND in Erlang.)

- In guards, use `,` and `;` if possible. Otherwise use `andalso`/`orelse`.

- In function bodies, use `andalso` and `orelse`.

- Use `or` if you need a strict OR operation.

Use cases for a strict OR are rare, but not as rare as use cases for a strict AND. Here is one example I can think of:

```erlang
foobar(A, B) when (A rem 8 =:= 0) or (B rem 8 =:= 0) ->
    at_least_one_integer_divisible_by_8.

```

That is, `A` and `B` must both be integers, and at least one of them must be a multiple of 8.

UPDATE: I wrote “short-cicuiting AND” when I meant “strict AND” in the paragraph where I recommended against using `and`’.

UPDATE: I wrote “short-circuiting OR” when I meant strict OR.

---

_[View the full topic](https://erlangforums.com/t/what-did-you-first-think-of-the-erlang-syntax/365)._
