← Back to context

Comment by dr_kiszonka

1 year ago

It looks like Lua's conditionals, for loops, and function definitions terminate with the "end" keyword. Is this considered more readable or does it bring some other benefits besides helping the interpreter parse code?

Many people love Lua, so I suspect there is a good reason for this.

It depends on which alternative you're thinking of.

- }, like C: Lua uses this to terminate tables. You could use the same token for both purposes (Perl5 does) but the reduced redundancy comes at some cost to both readability and syntax error reporting, which are especially important in languages aimed at driveby programmers, like Lua.

- ): Lisps generally just use ) to terminate every construct, reducing redundancy further.

- different end tokens for each construct, like Ada and the Bourne shell (endif/end if/fi): improves readability and error reporting further, but requires more reserved words and is wordier.

- end, like Lua: intermediate among the above choices.

- indentation, like Python: seems to work well enough in Python, and especially helps beginners who are therefore not permitted to indent their code incorrectly, and adds a maximum amount of redundancy for opening and closing constructs (every line is explicitly tagged with its syntactic depth) without reducing readability. However, this aspect of Python was novel when Python was introduced, remains controversial, and has not been widely adopted in new languages.

A funny thing about Lua's syntax is that it's not newline-sensitive, but semicolons are optional. This is valid Lua:

    function x(a, b)f(a)f(b)g = a b = a end

Wherever two things are juxtaposed that can't be part of a single statement, it tries to parse them as two statements. This syntactic looseness means that some syntax errors that would be easily detected in semicolon languages aren't easily detected in Lua. This increases the penalty for removing additional syntactic redundancy.

All programming languages have tiny differences. Some use }, some use keywords like "end" or "fi", some use indentation like python.

Its a trivial difference that does not matter.

Wikipedia says that came from Modula, but I can't find why Modula did it or why Lua decided to copy it.

I thought I read somewhere that Lua was meant for use by non-programmers, so the "end" would be easier to type and read than curly brackets.

  • Modula is based on pascal which is based on algol. The "end" thing is in the first version of algol from 1958. This is 14 years before C was invented.

    Presumably they thought the more verbose syntax would be more readable.

    • Algol (and Pascal) originally did the same thing that C did, except that you used `begin` and `end` instead of `{` and `}` when you needed a compound statement as a branch etc.

      Modula changed it so that all structured constructs implicitly introduced a compound statement, and terminated one where appropriate (e.g. on `else`), but this still required some end marker for the last one.

      If the question is why `begin` and `end` and not `{` and `}` - for one thing, Algol was designed to be the language you'd write reference implementations of algorithms in for publication, so readability was more important than terseness. For another, it predates ASCII, and not all charsets in use at the time even had `{` and `}` as symbols.