The alien erlang syntax choices


Some developers still argue about which punctuation is the best. Some would support the C syntax with semicolon ; at the end of each line, something that would look like:

int main() {
  int a = 6;
  int b = 3;
  if (a > b) // notice this line has no punctuation
    printf("a is greater than b \n");
  return 0;

Others will say the best syntax is a ruby/python-like with no punctuation. You can write in elixir (very close to ruby):

def print_file do
  # notice the punctuation in the method!("scratch.txt")
  |> Enum.each(&IO.puts/1)

As you can notice, both school still compromise with or without punctuation when they need to. So the answer is not as simple as a yes or no. There is even a language for which learning the punctuation is rather difficult when coming from other languages: Erlang.

Punctuation in Erlang

Read this snippet taken from stackoverflow and try to focus on the punctuation at the end of the lines.

foo(X) when X > 0; X < 7 ->
    Y = X * 2,
    case Y of
        12 -> bar;
        _  -> ook
foo(0) -> zero.

The answer is much clearer than me, but if you don't want to open the link, here's the explaination. There are 4 possibilities to end the lines:

  • The period . indicates the end a statement or function declaration. In the Erlang Repl, your would write A = 1. to execute the assignement
  • The comma , indicates an intermediary statement, like in a sentence, the statement is not the last one
  • The semicolon ; indicates that the current context is close but there is another related choice available. It is used both for multiple function declarations and conditionnal statements
  • Finally, only for conditional statement, you can finish last statements without no punctuation at all. I find that very confusing

I think punctuation is always here in some way and there is no right or wrong in it. Erlang syntax is easier to read once you get used to it, but it's also more error prone. It's a matter of balance and personal preference here. I hope you won't blame Java and C to have constraining syntaxes anymore!

Discuss on twitter