# The syntax thread!

**URL:** <https://forum.devtalk.com/t/the-syntax-thread/926>\
**Category:** General Dev Chat\
**Tags:** opinions, syntax\
**Created:** [27 June 2020 14:57 UTC](https://forum.devtalk.com/t/the-syntax-thread/926 "2020-06-27T14:57:00Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![AstonJ](https://forum.devtalk.com/user_avatar/forum.devtalk.com/astonj/32/7_2.png) [@AstonJ](https://forum.devtalk.com/u/AstonJ)\
**Post date:** [27 June 2020 14:57 UTC](https://forum.devtalk.com/t/the-syntax-thread/926/1 "2020-06-27T14:57:00Z")

</div>

😈

What is your preferred syntax style and why?

Perhaps we can add examples and use the code below as a simple reference point (how would you write it in your chosen language?) but feel free to add additional examples if you feel they show off your preferred syntax well too.

### Ruby

```ruby
class Ball
  def initialize(colour)
    @colour = colour
  end

  def red?
    @colour == "red"
  end
end

my_ball = Ball.new("green")
my_ball.red?
# false

```

---

<div class="post-metadata">

**Author:** ![davearonson](https://forum.devtalk.com/user_avatar/forum.devtalk.com/davearonson/32/591_2.png) [@davearonson](https://forum.devtalk.com/u/davearonson)\
**Post date:** [27 June 2020 19:58 UTC](https://forum.devtalk.com/t/the-syntax-thread/926/2 "2020-06-27T19:58:59Z")

</div>

One thing I like about the syntax of typical OO languages, and “pipelined” languages like Elixir, is the concept of “take this, do this with it, then do that with it, then do the other thing with it”. That concept really helped me pick up Elixir pretty quickly after realizing that `|>` was very much like `.` just without the implicit matching on whatever kind of thing (Ruby object, Elixir data) you had at that time.

---

<div class="post-metadata">

**Author:** ![hauleth](https://forum.devtalk.com/user_avatar/forum.devtalk.com/hauleth/32/599_2.png) [@hauleth](https://forum.devtalk.com/u/hauleth)\
**Post date:** [28 June 2020 09:38 UTC](https://forum.devtalk.com/t/the-syntax-thread/926/3 "2020-06-28T09:38:09Z")

</div>

Whatever people say I prefer languages with block delimiters instead of off-side rule. Off-side rule is okayish in configuration languages and other languages that describe trees (like HAML or Slime), but IMHO do not works well in programming languages (sorry Python, I never really liked you anyway).

---

<div class="post-metadata">

**Author:** ![OvermindDL1](https://forum.devtalk.com/user_avatar/forum.devtalk.com/overminddl1/32/52_2.png) [@OvermindDL1](https://forum.devtalk.com/u/OvermindDL1)\
**Post date:** [29 June 2020 15:29 UTC](https://forum.devtalk.com/t/the-syntax-thread/926/4 "2020-06-29T15:29:35Z")

</div>

Oh there are so many languages that I like so much syntax of, but I will start with FORTH, it has the ‘purest’ and most simple syntax, it’s just a stream of WORDS. Anything can be a word except whitespaces, whitespace delineates WORDS, and a WORD is any sequence of characters excluding whitespace. ^.^

A WORD you can think of as a function in FORTH, although whether that’s a built-in or so can be debatable, and you can rebind WORD’s, and WORD’s are only run one at a time, so a single WORD will be run to completion before it even looks at the next. You can write a fully conformant FORTH interpreter in just a few hundred lines of code in a verbose language (like a dozen or so in Python). FORTH runs on satellite’s even to this day to more things than people would expect, it is fast and short and easy to update.

The basic concept of FORTH is there is a STACK, which you can push to and pop from, though there are WORDS that allow you do to things like assign variables and such as well in most implementations.

For example `DUP` will duplicate what is on top of the stack, so if the number `1` was on it, now there will be two `1`'s. To put a `1` on the stack you can just use the WORD `1`, which defaults to a built-in function that just parses that integer and puts it onto the stack, all such integers are ‘defaulted’ in such a way, though in some implementations you can override that if you so wish. `+` is a built-in function (again, can be overridden by the user, but ew in this case?), it just pops the top two items off the stack and puts on to the stack the added value of the two things it popped off.

So if you run:

```auto
1 2 +

```

Then you will end up with `3` on the stack as it first pushes a `1` when it runs `1`, then pushes a `2` when it runs `2`, then it pops the `2` and then the `1` and pushes a `3` when it runs the `+` WORD.

To define a function you use the `:` WORD, which pops WORD’s from the input stream until it hits a `;` WORD and stores them into the named WORD. So a function like `: DOUBLE 2 * ;` would define a `DOUBLE` word that pushes a 2 then runs `*`, which will pop the top two stack values, multiply them together, and push on the result, so doing `3 DOUBLE` would then have 6 on the stack. You can optionally have a comment after the name in the function definition inside of `(...)` so by convention the stack input along with `--` then the stack output is generally put there, so for DOUBLE WORD you’d document it like `: DOUBLE (a -- b) 2 * ;` to state it pops one input and pushes one output, and of course you can write text to describe documentation as well. For note, even things like `;` are just functions too, though for `;` it’s `: ; POSTPONE EXIT REVEAL POSTPONE [ ; IMMEDIATE`, where the WORD `POSTPONE` means to take a WORD that you would normally call immediately and instead postpone it, like take the pointer to a function in most languages. EXIT means to exit compilation mode, REVEAL will reveal the new WORD so it can be used, etc… FORTH itself needs very very few built-ins defined (though there are usually a lot more for speed reasons), and even the act of defining a function is actually taking the function pointers of WORDS and inlining them, usually directly to machine code (often with some simple optimizations to inline things), hence why FORTH is usually quite FAST. ^.^

For note, FORTH is usually case insensitive, but full-caps is most often used by convention except in strings (`"` WORD).

Let’s see a simple popular tutorial set of code:

```auto
: STAR [CHAR] * EMIT ;
: STARS 0 DO STAR LOOP CR ;
: SQUARE DUP 0 DO DUP STARS LOOP DROP ;
: TRIANGLE 1 DO I STARS LOOP ;
: TOWER ( n -- ) DUP TRIANGLE SQUARE ;

```

This allows you to call, say, `4 TOWER` and it will print out:

```nohighlight
*
**
***
****
 ****
 ****
 ****

```

For each function:

- `: STAR [CHAR] * EMIT ;` This defines a WORD named `STAR`, the `[CHAR]` word takes the next WORD and treats it as a character array (basically a string), so `*` in this case, and then `EMIT`'s it to the screen. So calling `STAR` will just print a `*`. For note, `[CHAR]` is ansi forth setups rather than the FORTH standard, to be standard compliant just change `[CHAR] *` to `42`. ^.^
- `: STARS 0 DO STAR LOOP CR ;` This defines a WORD named `STARS`, then pushes a `0` onto the stack, then calls the WORD `DO`, which will pop the top two numbers off the stack and consume WORD’s up until LOOP is encountered, then run them repeatedly the number of times as the number popped off stack (0 to N, where N is what was popped), then lastly it prints a carriage return (`\n` in other words). So calling `3 STARS` will print `***\n`.
- `: SQUARE DUP 0 DO DUP STARS LOOP DROP ;` This defines a WORD named `SQUARE`, which first DUPlicates what’s on top of the stack, then pushes a 0, then does a loop of calling `STARS` that number of times after duplicating the input again, then drops the number that’s on the stack, so calling `2 SQUARE` would print ` **\n** \n`.
- `: TRIANGLE 1 DO I STARS LOOP ;` This loops from 1 to the passed in number and calls `STARS` with each iteration of `I` (the internal variable set by `DO` for its loop by default), so calling `4 TRIANGLE` would print `*\n **\n*** \n`, so a triangle of one size less than the number pass in
- `: TOWER ( n -- ) DUP TRIANGLE SQUARE ;` This just duplicates the input, then calls TRIANGLE (which pops one of those off) then calls SQUARE (popping the original off), and that’s it.

But FORTH is pure function, most programs in FORTH will read a lot like english sentences, you can define any kind of DSEL as you want, etc… FORTH and LISP have very similar ideas, both can create DSEL’s with impunity, but where I’d say FORTH is a purity of function, LISP is a purity of form, they do things in very different ways.

FORTH being so short and easy to implement even in raw machine code makes it really common to bootstrap micro-projects that need to be reprogrammable, plus it’s very fun to program in as it is so different than essentially any other language. ^.^

---

<div class="post-metadata">

**Author:** ![davearonson](https://forum.devtalk.com/user_avatar/forum.devtalk.com/davearonson/32/591_2.png) [@davearonson](https://forum.devtalk.com/u/davearonson)\
**Post date:** [29 June 2020 15:56 UTC](https://forum.devtalk.com/t/the-syntax-thread/926/5 "2020-06-29T15:56:02Z")

</div>

Ha, yeah, writing a (subset) FORTH interpreter is one of the exercises on some of the language tracks on [Exercism.io](http://Exercism.io). Lots of fun.

---

<div class="post-metadata">

**Author:** ![DevotionGeo](https://forum.devtalk.com/user_avatar/forum.devtalk.com/devotiongeo/32/1140_2.png) [@DevotionGeo](https://forum.devtalk.com/u/DevotionGeo)\
**Post date:** [30 June 2020 20:08 UTC](https://forum.devtalk.com/t/the-syntax-thread/926/6 "2020-06-30T20:08:02Z")

</div>

- I don’t like indentation based syntax, even if I like Elm.
- I wish Ruby, Crystal and Elixir had `{ }` instead of `do-end`, except the anonymous functions in Elixir.
- I wish parenthesis were not optional in Ruby, Crystal and Elixir, even if I leave it often now that they are optional.
- In my opinion Go’s syntax is the most clear (by clear I don’t mean elegant). I am also OK with Go’s `if err != nil { ... }` but I wish either Go had a ternary operator or `gofmt` allowed the error check on one line, like above.

---

<div class="post-metadata">

**Author:** ![OvermindDL1](https://forum.devtalk.com/user_avatar/forum.devtalk.com/overminddl1/32/52_2.png) [@OvermindDL1](https://forum.devtalk.com/u/OvermindDL1)\
**Post date:** [30 June 2020 20:24 UTC](https://forum.devtalk.com/t/the-syntax-thread/926/7 "2020-06-30T20:24:03Z")

</div>

Have you looked at rust? Not indentation based, uses `{`/`}`, parenthesis are not optional, etc… ^.^

---

<div class="post-metadata">

**Author:** ![DevotionGeo](https://forum.devtalk.com/user_avatar/forum.devtalk.com/devotiongeo/32/1140_2.png) [@DevotionGeo](https://forum.devtalk.com/u/DevotionGeo)\
**Post date:** [30 June 2020 20:42 UTC](https://forum.devtalk.com/t/the-syntax-thread/926/8 "2020-06-30T20:42:43Z")

</div>

Yes, I’m currently learning Rust, and so far I’m liking it. I don’t give it much time though. I started with [Manning - Rust in Motion](http://www.manning.com/livevideo/rust-in-motion). The learning curve is of course steep, but the language is powerful.

---

<div class="post-metadata">

**Author:** ![Korbin73](https://forum.devtalk.com/user_avatar/forum.devtalk.com/korbin73/32/827_2.png) [@Korbin73](https://forum.devtalk.com/u/Korbin73)\
**Post date:** [9 July 2020 17:02 UTC](https://forum.devtalk.com/t/the-syntax-thread/926/9 "2020-07-09T17:02:30Z")

</div>

Hands down my favorite syntax is ML family of languages ocaml/f#/haskell/Elm

```auto
let registerNewUser dbClient =
    request ( fun inputGraph ->
      Newtonsoft.Json.JsonConvert.DeserializeObject<UserRequest>(inputGraph.rawForm |> System.Text.ASCIIEncoding.UTF8.GetString)
      |> hashPassword
      |> registerWithBson dbClient
      |> RealWorld.Convert.userRequestToUser
      |> jsonToString
      |> Successful.OK
    )

```

Why? Whitespace is a natural idiom in written languages. We use indentation in table of contents, spaces between words, paragraphs, etc. So adding glyphs like brackets or begin…end in a language is completely redundant. In ML family languages all symbols have contextual meaning. Parenthesis are used to group “stuff” together. Brackets are added when the need to indicate a “block” only in certain contexts.

Simply put, if whitespace significance didn’t matter in programming languages, then remove all of the spaces in your C family languages that the compiler is happy to build and see if you like it LOL

That being said, I don’t dislike C-family languages (I program in swift and kotlin on my job) but ML syntax is definately the most pleasant for me to read due to the low visual “noise”.

---

<div class="post-metadata">

**Author:** ![dimitarvp](https://forum.devtalk.com/user_avatar/forum.devtalk.com/dimitarvp/32/5599_2.png) [@dimitarvp](https://forum.devtalk.com/u/dimitarvp)\
**Post date:** [10 July 2020 21:21 UTC](https://forum.devtalk.com/t/the-syntax-thread/926/10 "2020-07-10T21:21:15Z")

</div>

I definitely agree with you – I found OCaml very pleasant. But still, only using whitespace to mentally parse code is much harder for me compared to the block-y and glyph-y delimiters in other languages.

Maybe it’s about a habit though.

---

<div class="post-metadata">

**Author:** ![mafinar](https://forum.devtalk.com/user_avatar/forum.devtalk.com/mafinar/32/723_2.png) [@mafinar](https://forum.devtalk.com/u/mafinar)\
**Post date:** [11 July 2020 03:06 UTC](https://forum.devtalk.com/t/the-syntax-thread/926/11 "2020-07-11T03:06:07Z")

</div>

I started learning Rust with the same resource. So far it’s doing a good job at explaining Rust to me and I am loving the syntax. I’ll start reading Rust in Action once I am done.

---

<div class="post-metadata">

**Author:** ![aenglisc](https://forum.devtalk.com/user_avatar/forum.devtalk.com/aenglisc/32/536_2.png) [@aenglisc](https://forum.devtalk.com/u/aenglisc)\
**Post date:** [12 July 2020 06:03 UTC](https://forum.devtalk.com/t/the-syntax-thread/926/12 "2020-07-12T06:03:22Z")

</div>

ML languages definitely have the best syntax as far as I’m concerned, however I’ve also grown to enjoy Erlang’s syntax.

---

<div class="post-metadata">

**Author:** ![OvermindDL1](https://forum.devtalk.com/user_avatar/forum.devtalk.com/overminddl1/32/52_2.png) [@OvermindDL1](https://forum.devtalk.com/u/OvermindDL1)\
**Post date:** [13 July 2020 22:30 UTC](https://forum.devtalk.com/t/the-syntax-thread/926/13 "2020-07-13T22:30:18Z")

</div>

> [@dimitarvp](#):
>
> I definitely agree with you – I found OCaml very pleasant. But still, only using whitespace to mentally parse code is much harder for me compared to the block-y and glyph-y delimiters in other languages.

OCaml doesn’t use any whitespace for anything other than to separate tokens. There is no significant whitespace (excepting one odd annoying bit about top level `let`’s where `in`’s are optional…).

* * *

Erlang’s Prolog’ish style syntax is nice though yeah.

---

<div class="post-metadata">

**Author:** ![Kurisu](https://forum.devtalk.com/user_avatar/forum.devtalk.com/kurisu/32/1106_2.png) [@Kurisu](https://forum.devtalk.com/u/Kurisu)\
**Post date:** [14 July 2020 12:14 UTC](https://forum.devtalk.com/t/the-syntax-thread/926/14 "2020-07-14T12:14:10Z")

</div>

I really like Elixir pipe operator `|>`.

I know one can easily abuse of it and that we should not pipe to pipe… but sometimes I can’t help. 😅  
Here is an example that I use in my Ecto Contexts to generate some a rank that will be used to sort a collection like for example “categories list”. By default when adding a new item to the collection, the function will check the higher rank in a given DB Table, then add `+1`. The form will be populate with this rank but the user can change it then or later. But too much talk, here is the function. 😅

```auto
  def get_auto_rank(schema) do
    from(s in schema, select: max(s.rank))
    |> Repo.one()
    |> Kernel.||(0)
    |> Kernel.+(1)
  end

```

---

<div class="post-metadata">

**Author:** ![dimitarvp](https://forum.devtalk.com/user_avatar/forum.devtalk.com/dimitarvp/32/5599_2.png) [@dimitarvp](https://forum.devtalk.com/u/dimitarvp)\
**Post date:** [14 July 2020 18:00 UTC](https://forum.devtalk.com/t/the-syntax-thread/926/15 "2020-07-14T18:00:42Z")

</div>

> [@Kurisu](#):
>
> I know one can easily abuse of it and that we should not pipe to pipe… but sometimes I can’t help. 😅

I completely sympathize, I abused it for a while before I stopped. 😃

That being said, IMO your code will be more readable and self-explanatory if written like so:

```elixir
def get_rank(schema) do
  from(s in schema, select: max(s.rank))
  |> Repo.one()
end

def get_rank_or_default(schema) do
  (get_rank(schema) || 0)
  |> Kernel.+(1)
end

```

At least that’s what I would say if I was code-reviewing you. 🙂

---

<div class="post-metadata">

**Author:** ![davearonson](https://forum.devtalk.com/user_avatar/forum.devtalk.com/davearonson/32/591_2.png) [@davearonson](https://forum.devtalk.com/u/davearonson)\
**Post date:** [14 July 2020 18:15 UTC](https://forum.devtalk.com/t/the-syntax-thread/926/16 "2020-07-14T18:15:01Z")

</div>

Not:

```auto
def get_rank_or_default(schema) do
  (get_rank(schema) || 0) + 1
end

```

? Am I missing something that would make that not work?

---

<div class="post-metadata">

**Author:** ![dimitarvp](https://forum.devtalk.com/user_avatar/forum.devtalk.com/dimitarvp/32/5599_2.png) [@dimitarvp](https://forum.devtalk.com/u/dimitarvp)\
**Post date:** [14 July 2020 18:26 UTC](https://forum.devtalk.com/t/the-syntax-thread/926/17 "2020-07-14T18:26:39Z")

</div>

It will work, of course. What I offered is a compromise between brevity and immediate readability – quite the subjective one too. Yours can also be treated as very readable!
