Syntax styles in programming languages

Last edited May 2021
Backlinks: Programming language design

This is a collection of different syntactical styles in programming languages. It’s woefully incomplete.

Comments

Single line comments:

Multi-line comments:

Multi-line comments might not really be that useful.

Indentation and bracing

Braces (C, Java, … style):

stuff {
  thing
}

doend (Ruby style):

stuff do
  thing
end

Explicitly naming the thing to end:

stuff
  thing
end stuff

There’s also bash’s bizarre way of ending statements, with reverse keywords like elihw and esac, but that’s just silly.

Calls

Regular math-style or C-style function calls:

cos(pi)
plus(2, 3)

Haskell-style calls (without parentheses):

cos pi
plus 2 3

OO-style calls (Smalltalk style):

pi cos
2 plus: 3

OO-style calls (Ruby style, i.e. no parentheses needed):

pi.cos
2.plus(3)

This approach makes it non-trivial to get hold of the method as an object.

OO-style calls (JavaScript style):

pi.cos()
2.plus(3)

Getting hold of the method itself is simple in this case. (The question of how the method is bound remains. If you get the method, is it still bound to the original object, or not? If not, how do you call it? Do you pass self as the first argument, perhaps? )

Concatenative style (like Forth and Factor):

pi cos
2 3 plus

To do: Describe named parameters

Function definitions

No braces, implicit return:

fun greeting()
  "Hello!"
end

The parentheses, if no parameters are available, could be left out:

fun greeting
  "Hello!"
end

Braces, explicit return:

fun greet() {
  return "Hello!"
}

Braces, shorthand, implicit return:

fun greet() = "Hello!"

With positional parameters:

fun greet(name)
  return "Hello, ${name}"
end

With default value, assigned using = (Python style):

fun greet(name = "stranger")
  return "Hello, ${name}"
end

With default value, assigned using : (Ruby style):

fun greet(name: "stranger")
  return "Hello, ${name}"
end

Using : can conflict with type declarations.

To do: Describe variable-argument definitions

Method definitions

To do: associated functions

To do: instance variables

To do: self (this instance), e.g. this.doSomething()

To do: self (this type), e.g. new thisClass

Strings

Double-quoted:

"Hello"

With interpolation, Ruby style:

"Hello, #{name}"

With interpolation, Python style:

f"Hello, {name}"

With interpolation, JavaScript style:

`Hello, ${name}`

To do: Characters

Iteration

I’m leaving out old-school C-style for loop iteration because I think it’s too old-school.

Dedicated for construct:

for elem in iterable {
    // …
}

Function:

iterable.each(func (elem) {
    // …
})

Note that the above can’t use return to break out of the loop. Smalltalk-style non-local returns would allow that, but it’d return from the function entirely:

"a strange way to get the first element of an iterable"
iterable do: [:elem | ^elem]

Ruby has blocks and functions, and thus has nonlocal returns and can break out of blocks. Ruby also has a ton of custom iterators, like #times:

12.times do
  # …
end

Ranges

Equality

Ugh! This one is complicated. Common ways to check for equality are:

Different types of equality to take into account:

Ruby’s === is confusing (“case equality” is ambiguous and hard to explain).