27
$\begingroup$

I'm not aware of any language other than Python that uses indentation to denote code blocks. To me, it seems like the simplest way to indicate blocks, and it also encourages proper formatting by making code not work if it is not indented properly. I would expect it to be more widely used; are there drawbacks that I'm missing?

$\endgroup$
6
  • 13
    $\begingroup$ Haskell, occam, ABC, and various markup and data serialisation languages also use significant indentation. $\endgroup$
    – Adám
    May 16, 2023 at 17:39
  • 2
    $\begingroup$ Scala can use indentation instead of braces $\endgroup$
    – Seggan
    May 16, 2023 at 17:45
  • 1
    $\begingroup$ Not a programing language but YAML is another one $\endgroup$
    – mousetail
    May 16, 2023 at 20:27
  • 6
    $\begingroup$ Scala 3 lets you use both indentation and braces, so you can experience the worst of both worlds! $\endgroup$
    – user
    May 17, 2023 at 20:41
  • 2
    $\begingroup$ Makefiles, although not a programming language, also use indentation. $\endgroup$
    – gerrit
    Jul 19, 2023 at 13:08

13 Answers 13

31
$\begingroup$

Writing a book

Writing a book with code blocks in this language will be difficult, because between two pages, if a code block is cut in half, readers will have a hard time to figure out how many indentations the second page starts with.

$\endgroup$
3
  • 9
    $\begingroup$ Can confirm. Worse is that some platforms will render PDFs where indentation is achieved by moving the code to the right rather than by inserting spaces. This means that copying code out of the PDF removes all indentation, because it was all just visual. $\endgroup$
    – l0b0
    May 16, 2023 at 23:00
  • 3
    $\begingroup$ Who writes code in books nowadays? And besides, you'd just implement feint vertical ruled lines. $\endgroup$
    – Steve
    Jul 8, 2023 at 18:05
  • 5
    $\begingroup$ Even with braces, keeping track of nesting levels across page breaks is difficult. Humans don't generally find it easy to keep track of brace levels, that's why we expect code to be indented when the language uses braces. $\endgroup$
    – Barmar
    Oct 20, 2023 at 15:50
31
$\begingroup$

Awkward code pasting

Copying/Cutting and pasting code becomes less than trivial, since the new location may need less or more indentation. An IDE that's aware of the language in use may be able to help with this, but plain text editors cannot.

In addition, various potential pasting destinations fail to preserve whitespace, e.g. comments on Stack Overflow, and messages on Zoom. Facebook and other sites often do not preserve whitespace, so it becomes harder to discuss code examples with others online.

On various forums, inexperienced users tend to paste in their code without putting it in a code block, so as usual with HTML, any amount of consecutive whitespace gets stripped down to a single space, completely altering the meaning of the code.

Finally, some languages, like Python, make the situation even worse than it could be because code blocks are not variable scopes. Moving a block of code means that any variables that are logically "local" to that block don't always hygienically move with it. JavaScript also has this problem without significant whitespace, but the combination in Python is particularly problematic.

$\endgroup$
15
  • 2
    $\begingroup$ So the idea is that you can copy the code, test it, then fix indentation if it works? The fact that indentation needs to be fixed when copying is one of the things that I see as a benefit of significant whitespace because it avoids incorrect indentation. $\endgroup$
    – Someone
    May 16, 2023 at 17:43
  • 1
    $\begingroup$ @NikeDattani because they are not connected in any way. $\endgroup$
    – Adám
    May 16, 2023 at 18:02
  • 1
    $\begingroup$ Also worth adding that pasting code into e.g. comments on Stack Overflow, Facebook or other sites often does not preserve whitespace, so it becomes harder to discuss code examples with others online. $\endgroup$
    – kaya3
    May 16, 2023 at 21:08
  • 1
    $\begingroup$ Also, often on forums, people paste in their code without putting it in a code block, so as usual with HTML, the double spaces get stripped, removing the indentation. It's annoying when people do this but people do what they do. $\endgroup$
    – naffetS
    May 18, 2023 at 1:41
  • 1
    $\begingroup$ Editors for languages with more restricted syntax than C can auto-format copy/pasted code so that indentation will match program semantics, allowing a programmer to look at the indentation and confirm that things are correct. Having indentation auto-adjust to match other syntactic markers is more robust than having it be syntactically significant in and of itself. $\endgroup$
    – supercat
    Oct 18, 2023 at 18:10
21
$\begingroup$

Tabs and Spaces

In languages that support both tabs and spaces for indentation (*cough* Python), the compiler/interpreter often gets angry if you accidentally mix them, and keeping the one your project uses consistent can be hard without, or sometimes even with, a proper IDE.

$\endgroup$
5
  • 10
    $\begingroup$ IMO this isn't an argument against using indentation for code blocks, it's an argument for not allowing programmers to mix tabs and spaces. :P $\endgroup$
    – DLosc
    May 16, 2023 at 19:55
  • 3
    $\begingroup$ @DLosc Sometimes it's not intentional; I myself have frequently pressed Tab in unfamiliar IDEs, expecting it to be auto-converted to spaces, and been sorely disappointed several times. $\endgroup$
    – Ginger
    May 16, 2023 at 19:59
  • $\begingroup$ Fair. It's too bad tabs vs spaces hasn't gotten to a similar place as different newline conventions, where it's fairly easy to convert between the different styles. $\endgroup$
    – DLosc
    May 16, 2023 at 20:11
  • $\begingroup$ @DLosc: IMHO, it would have been helpful if programming languages had recognized some meta constructs that would have allowed files to contain information about how editors should process them which would be transparent to the rest of the language. One of the things I liked about 1990s Macintosh was that each file had a resource fork and a data fork, and text editors could store things like tab stop settings in a resource fork that would be transparent to programs like C compilers. Additionally, if an editor with an assigned application signature stored its settings in a resource... $\endgroup$
    – supercat
    Oct 19, 2023 at 16:15
  • $\begingroup$ @DLosc: ...whose type matched that signature, it would be transparent to any program that didn't understand that resource type. Things weren't perfect, but being able to have text files include such information meant that a text document that was intended to be displayed with two-character tab stops would configure a text editor that understood its setting resource to use two-character tab stops. $\endgroup$
    – supercat
    Oct 19, 2023 at 16:17
21
$\begingroup$

Limited auto-formatting

Complete auto-formatting isn't possible. For a language where proper indentation and/or code layout can be inferred from the non-whitespace syntax, it is possible to completely automate formatting. Adjusting braces or keywords (only two per block) is often less work than aligning all the lines in the block.

$\endgroup$
8
  • 5
    $\begingroup$ Languages like python do have auto formatting and it's used extensively. Formatting has less to do though since code that's syntactically valid is already somewhat properly formatted $\endgroup$
    – mousetail
    May 16, 2023 at 18:24
  • 2
    $\begingroup$ @mousetail Right, but I tend to make a mess when editing my code, and then let the auto-formatter fix it all :-P $\endgroup$
    – Adám
    May 16, 2023 at 18:26
  • 1
    $\begingroup$ I do the same in python. Often I leave blocks with too much indentation after, for example, removing a nested if, and the formatter will fix it for me. The formatter will also fix anything inside parenthesis. $\endgroup$
    – mousetail
    May 16, 2023 at 18:27
  • 1
    $\begingroup$ @mousetail amended accordingly $\endgroup$
    – Adám
    May 16, 2023 at 18:31
  • 4
    $\begingroup$ How is this different from not being able to format C++ code with broken bracket nesting? $\endgroup$
    – l0b0
    May 16, 2023 at 23:01
16
$\begingroup$

One-liners and code golf

It will be difficult to write simple one-line code if code blocks must start at the second line. In CLI (command-line interface) applications, there are many use cases like <compiler> -e "one line expression", which can be hard if the language requires indentation in code blocks.

$\endgroup$
2
  • 3
    $\begingroup$ Appropriate considering most of us come from Code Golf.SE $\endgroup$
    – Seggan
    May 16, 2023 at 19:16
  • 2
    $\begingroup$ Some languages solve that by having more than just newline as a separator token, often backslash or semicolon. Eg. while x; f(x); g(x+1); end $\endgroup$
    – Longinus
    May 20, 2023 at 0:42
15
$\begingroup$

Accessibility

For blind and visually impaired (B/VI) programmers, using indentation-sensitive languages with a screenreader has some extra challenges compared to free-format languages.

The most popular screenreaders are NVDA (free) and JAWS (commercial). You can configure them to speak whitespace, but it’s very noisy to hear all spaces, so it’s typical to set them to only speak the indentation level at the beginning of a line, particularly when moving the cursor to that line.

Example

So for example, in this code:

# Completion message

if work_complete:
    print("All done!")

A typical NVDA setup reads these lines as:

  1. no indent number completion message
  2. no indent blank
  3. no indent if work line complete colon
  4. four space print left paren, quote all done bang, quote right paren

And following are some observations about that.

Presentation vs. Semantics

Notice that the reader says “four space”, “eight space”, “twelve space”, which you need to mentally translate to “one indent”, “two indent”, “three indent”.

Perhaps that’s an argument for the use of tabs exclusively for indentation, or other ways of indicating nesting, or even avoiding it altogether.

Keyboard Navigation

Blind programmers tend to navigate entirely by keyboard, which is more convenient when there are paired delimiters at the start and end of a block. In Vim for example you can jump back and forth between brackets with %, and in Emacs you can use M-n forward-sexp and M-p backward-sexp. If I type a bracket that matches something far away (offscreen), Emacs gives a message saying what it matched.

I’m not blind, but I do use a keyboard almost exclusively. When programming indentation-based languages in Emacs, I find that I mainly need to navigate by jumping between blank lines instead (using the “paragraph” commands M-{ and M-}).

Punctuation Noise

Punctuation characters are pronounced! Overall, Python is pretty good about this, despite the indentation, because it has relatively few and simple combinations of symbol characters, preferring words for common tasks.

Still, some symbols could be confusing. The comment delimiter of “number” can sometimes sound like a word that’s meant to be part of the comment text. ASCII unpaired quotation marks don’t tell you whether you’re at the start or end of a string.

Other symbols are just annoying. Python’s snake_case (“snake line case”) convention is slightly more readable for sighted users, because it reduces regressive saccades caused by missing the word boundary when scanning. But it’s less convenient for screenreaders, which deal better with camelCase (“camel case”) since that’s a reasonable default for non-code text.

Resilience

If indentation is the only way of indicating block structure, as in Python, then the lack of redundancy means that an auto-formatter can’t reliably recover the structure from non-indented code.

And for example, if you’re listening to a video about Python, and you can’t see the spacing, then you have to go and find the text file so you can hear what the spacing actually is.

Alternatives

Optional Layout

In some languages like Haskell, indentation is optional, in that it’s syntactic sugar for explicit braces and semicolons. This was intended to make it easier to generate Haskell code, but it has the side benefit that you don’t need to use layout if it’s inconvenient for you. In principle, with an autoformatter, you could view and edit the files with explicit delimiters, but check them in with just indentation. I’d really like to have a language setting NoLayoutRule that disables it entirely! With such an option, you could enforce it throughout a codebase.

Braille Displays

Some of these factors are less of an issue with Braille displays, but those still aren’t in widespread use—there have been some advances in the past couple decades, but consumer devices are still quite costly—a few thousand USD. A Braille display has much lower bandwidth than a text-to-speech screenreader, due to the limited size—a single line of 40 or rarely up to 80 characters—and limited refresh rate—at best, 20ms or 50fps. Their chief benefit over a screenreader is precision and usability by deaf & hard-of-hearing (DHH) people.

$\endgroup$
1
  • $\begingroup$ Thanks for adding this interesting perspective! Are tabs better than spaces for screen readers? One tab equals one indentation level, while number of spaces depend on coding style and requires mental arithmetic to get the indentation level. Eg. tab tab print quote hello world quote vs eight space print quote hello world quote. $\endgroup$
    – ximo
    Feb 21 at 17:31
13
$\begingroup$

Source code size

For interpreted languages, where the source needs to be sent over the Internet, like JavaScript, source code size is incredibly important. Having the option to just remove all white-space without losing meaning can save a lot of bandwidth in these cases.

$\endgroup$
3
  • 8
    $\begingroup$ But it also compresses extremely well(?). Representable by a few bits. And repeats (low entropy). $\endgroup$ May 17, 2023 at 11:22
  • 3
    $\begingroup$ I'm not aware of anything over (at least) the past decade that sends the JS code not gzipped. Yes, minifiers are often employed to reduce the size of the code, however, like @PeterMortensen says, the whitespace should compress extremely well. Minifiers do remove whitespace but they produce more value by converting other parts of the code. $\endgroup$
    – VLAZ
    May 18, 2023 at 8:00
  • $\begingroup$ It's true compression will take out most of the bytes from the indentation but not all of them. It still makes a difference for big source files $\endgroup$
    – mousetail
    May 18, 2023 at 8:04
13
$\begingroup$

There is more than one way to define layout-sensitive programming languages, where indentation matters. The Python way is different from the Haskell way, which is different from the F# way... For reference see Michael Adams's Principled Parsing for Indentation-Sensitive Languages

For Python in particular, one of the biggest drawbacks of significant indentation is that lambdas cannot contain statements.

# Not allowed!
f = lambda x:
      if x: 
       print("hi")   

The reason for this is that Python ignores any indentation between parenthesis or brackets. This is how Python allows us to write multi-line expressions:

x = (1 + 
   2 + 
   3 )

Which Python treats as if you had written everything in the same line:

x = (1 + 2 + 3)

This means that expressions in Python cannot contain anything that cares about indentation. In particular, lambda expressions, cannot contain statements such as if or while, because those statements need indentation.

$\endgroup$
3
  • 3
    $\begingroup$ fn()=>Lambdas can not contain statements in PHP either even though it has bracket indentation. I don't think this choice is linked to the indentation system $\endgroup$
    – mousetail
    Jul 4, 2023 at 19:28
  • $\begingroup$ @mousetail It's very much related to the use of indentation for blocks. See this other Q&A. $\endgroup$
    – kaya3
    Oct 19, 2023 at 13:00
  • $\begingroup$ Thanks for the reference! Really useful for understanding how Haskell and Python parses indentations. $\endgroup$
    – ximo
    Feb 21 at 17:37
8
$\begingroup$

This isn't a full answer, just something I immediately thought of. The confusion between which style of indentation to use (spaces vs. tabs) and whether it's enforced is something to consider.

For example, Makefiles only allow tabs, while Ninja only allows spaces. Even Zig, not even being a whitespace-sensitive language, considered it an error to use tabs instead of spaces as indentation (Note: this was changed and now not an error, although the built-in formatter (and the only one, really) only allows spaces)

What if you don't have a formatter installed for the language? Different files in the same project, or even parts of a single file could use different styles. This applies of course not only to indentation-based languages, but it might impact them more. Imagine merging two blocks that used two different styles, for example. The lexer/parser has to somehow determine what level of indentation each block is. What if a user's editor is displaying tabs as 8 spaces, but the language (or even another user) treats them as 4 spaces?

Another thing to consider: In my experience, beginners have a hard time writing indentation based code when starting to code, but it teaches them to indent code correctly. You've probably seen lots of terrible C code that doesn't get indented at all in most places...

$\endgroup$
2
  • $\begingroup$ I have seen much code in non-indentation-aware languages, and yeah I haven’t “seen lots of terrible code” $\endgroup$
    – Seggan
    May 16, 2023 at 19:21
  • 1
    $\begingroup$ @Seggan "lots" and "terrible" was an exaggeration, yeah. mostly beginner students or profs at universities do this kind of styling afaik $\endgroup$
    – monomere
    May 16, 2023 at 20:09
7
$\begingroup$

Restricted layout

Freedom to indent without affecting meaning allows creative use of whitespace to illustrate structure and symmetries.

$\endgroup$
2
  • 1
    $\begingroup$ Example, since comments don't allow code blocks. $\endgroup$
    – Bbrk24
    May 16, 2023 at 21:43
  • 1
    $\begingroup$ @Bbrk24 This use-case is allowed in Python because the indentation occurs inside square brackets in an expression, rather than at the statement level. This might be an issue in other languages, though. $\endgroup$
    – kaya3
    Jun 2, 2023 at 14:47
6
$\begingroup$

Punch Cards and Coding Sheets

Punch cards are limited to 80 columns. When you start adding white space on the left, you soon run out of space on the right.

Of course, if you're a business analyst, you aren't punching your own cards. You're writing the code on an 80 column coding sheet. It's formatted for FORTRAN and COBOL indentation, but if you start adding more white space on the left, the punch operators have to count the spaces to get the indentation correct. And if they get it wrong (they will - they operate at speed), then you have to correct and resubmit -- which adds another day or two to the turn-around.

$\endgroup$
1
  • $\begingroup$ Hate it when that happens. $\endgroup$
    – ximo
    Feb 21 at 17:39
4
$\begingroup$

Bad for frameworks using inline code

Some languages such as PHP, JSP, ASPX, could be embedded in an HTML file. Sometimes people come up with the idea of doing similar things using Python. It would be awkward to keep the Python style indentation in the HTML file, so most would probably choose to use a simplified version that doesn't keep the full ability of Python. It would be much more awkward, if HTML was also using indentation this way.

A similar scenario is, some software may choose to use Python code in its configuration file, which is very reasonable. And some software may choose to not use a complete Python script, but mix the code with a traditional ini-style file. That is, one specific item could be set to a string which contains some Python code. Not being able to do this in one line may make the configuration file seemingly unnecessarily complex. It might be worse if the configuration file could contain arrays.

$\endgroup$
3
$\begingroup$

Introduction

When I first found out that I would be using FORTRAN 77 for a summer job that I had when I was a student, I asked friends if they had any experience with FORTRAN. I would often hear them say things like:

"FORTRAN is such a nasty language. Indentation actually matters. Can you believe it? Indentation matters! You are not going to be enjoying this summer, good luck!

Probably the biggest change between FORTRAN 77 and Fortran 90 (which still remains probably the biggest change between FORTRAN 77 and Fortran 2018) is that in Fortran 90 and newer version, indentation no longer matters.

Python didn't become popular until many years later, and I was shocked to see that "indentation matters" in Python. I thought Python was supposed to be a "modern" language? Why is it still like FORTRAN 77 in which indentation matters?

Answer: Instability against accidental errors

The drawbacks of a programming language using "indentation" to have any meaning other than just regular white space (which can be used to beautify or format the code in a readable way), become very apparent when you code in FORTRAN 77 and you realize how often you get a compile-time error due to the indentation being incorrect by just one character.

This is why they made it so that indentation no longer matters in Fortran 90 and later versions.

Answer: Higher barrier to entry

Another drawback is that in almost all other languages, whitespace or lack of it, does not carry such significant meaning, so when people try to copy your code and edit it, or when they try to write their own version of some snippet that they see on Stack Overflow, if they are not already experts they might ignore certain amounts of whitespace for aesthetic reasons, and they'll wonder why they're getting errors all the time. It saves a lot of people time if the language sticks to extremely well-adopted conventions, such as indentation not mattering.

Why it's not a problem for most Python Coders

Python programmers these days often use editors/IDEs like PyCharm and VS Code, but these do not work on all platforms, for example if you want to run calculations on a remote high-performance supercomputer that has nothing but a simple text-based command-line terminal, and no opportunity to output graphics to your computer screen. On such machines, the administrators will not give you "root" access to install PyCharm or VS Code. You are stuck with using Vi/Vim or maybe Emacs or Nano. In such an environment, soon you will notice how error-prone it is, to code in a language in which tabs and spaces are part of the actual syntax.

$\endgroup$
15
  • 6
    $\begingroup$ One case of a language making indentation no longer matter does not prove significant white space is bad $\endgroup$
    – mousetail
    May 16, 2023 at 18:57
  • 5
    $\begingroup$ There are a lot of languages where indentation does matter $\endgroup$
    – mousetail
    May 16, 2023 at 19:07
  • 1
    $\begingroup$ Many people prefer languages that have significant indentation, some prefer those that don't $\endgroup$
    – mousetail
    May 16, 2023 at 20:23
  • 2
    $\begingroup$ I don't know numbers. My main point was not that more people prefer indentation, just that both groups exist $\endgroup$
    – mousetail
    May 16, 2023 at 20:31
  • 4
    $\begingroup$ I write both Haskell and Python without using IDEs, and the indentation is not really difficult or error-prone at all. Vim is totally up to the task. (What is true is that Python's particular way of using indentation has some problems, and let's not even begin about Fortran 77, but none of this is helped with by IDEs.) $\endgroup$ Jul 4, 2023 at 17:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .