Nested Syntax: Brackets vs Begin/End vs Indentation

By Xah Lee. Date: . Last updated: .

Here's 3 programing language syntax for nesting.

Bracket style:

[11, [21, 22], 31]

Begin/End style:

begin 11, begin 21, 22 end , 31 end

Indentation style:


which is better?

Of all characters, there's a unique property of brackets such as ( ) [ ] { }. [see Unicode: Brackets, Quotes «»「」【】《》] Namely, they embed info on matching another char in text. This means, any editor, language, or parser, can trivially extract such info. Lisp uses this to advantage, and from it came widely praised lisp macros. Similar with JSON.

languages that use {begin, end}, such as {Ruby, Pascal}, means there need to be semantic info added to such string, and a lot work for parser or text editor to understand the nesting structure. Also, the nesting structure is less easy to recognize visually than brackets.

languages using indentation for blocks and nesting, such as {Python, Haskell, HAML, YAML, Slim} is non-trivial to parse. It requires a dedicated parser to understand the nesting.

Indentation syntax is also complex, prone to invalid syntax, because the nesting info must be embedded in each and every line of a nested structure.

Indentation syntax has another problem, which is most serious: Namely, it mixes semantic and syntactical info. The source code is hard-formatted in one fixed way, and must always be manually formatted. For a damage due to this, see: Why Python Lambda is Broken and Can't be Fixed.

See also: [Layout Syntax Considered Troublesome By Yin Wang. At , accessed on 2016-01-04 ]