4

A long time ago when starting learning programming I remember a teacher saying that compiler do not care about whitespace and they are useful to make code readable for human beings. It made perfect sense back then, but I only knew some Pascal and C++.

Many years later, after diving in many other languages I began wondering what is the rational of having programming languages that rely on indentation / white space, as there are quite a few disadvantages:

  1. Stange errors like this one (unindent does not match any oute indentation level)
  2. Minifying still requires extra whitespace - example
  3. Many other reasons as indicated in this Quora post:

    • lack of white space indentation across operating systems / text editors
    • whitespace might be introduced while doing code merge
    • possible errors while copy-pasting
    • all modern IDEs automatically indent code

Question: Why do we still have programming languages that rely on indentation / white space?

8
  • In which quora post?
    – max630
    Commented Apr 30, 2019 at 7:02
  • Statements must be separated in some way. Some (older) languages tend to use brackets or (BEGIN END) or semicolons for this, while newer languages use newlines. For languages like Kotlin, newline is enough, but other languages such as Python make indentation part of the language itself and therefore necessary. Ultimately, there are no serious advantages or disadvantages doing it one way or the other. It's ultimately to separate statements in a program.
    – Neil
    Commented Apr 30, 2019 at 7:45
  • @Neil actually older languages such as fortran made use of syntactic whitespace.
    – Ewan
    Commented Apr 30, 2019 at 8:45
  • @max630 - sorry, I have fixed that. Thanks.
    – Alexei
    Commented Apr 30, 2019 at 8:47
  • 1
    This question is subject of a meta post.
    – Doc Brown
    Commented Apr 30, 2019 at 10:40

3 Answers 3

19

What do you mean, "still"? Whitespace-delimiting is an advanced feature.

Lisp, Algol, Pascal, C etc. etc. were delimited by BEGIN..END or by bracket characters. The focus in early programming languages was to get the computer to do what we wanted at all. It mattered little if the formatting looked clunky.

But then programming became a huge success story, and programs, modules, and entire systems became huge and unwieldy. Eventually we realized that making the code look good for human readers was just as important as making it understandable for computers, because no one would be able to maintain old code otherwise. Mirroring logical structure (nesting) with indentation turned out to be a very good way of achieving this. Proper indenting became a standard rule of clean code.

Eventually people realised that the indentation could be used to inform both programmers and computers: with proper indenting - which every style guide already said you should be using anyway - you could in fact omit Pascal's BEGIN..END, or Lisp's wastelands of parentheses, or C/Java's brackets, and readability would be even better. This last point is controversial, but historically, whitespace delimitation is the endpoint of a development, not a remnant.

5
  • 7
    sure requiring you to see the difference between 2 spaces and 3 spaces in a sea of whitespace is totally easier to read
    – Ewan
    Commented Apr 30, 2019 at 8:17
  • 7
    @Ewan What language requires that?
    – Kyralessa
    Commented Apr 30, 2019 at 8:35
  • @Kyralessa I guess Python would if you decided to indent your blocks with just 1 space (it'd require you to be at least a little consistent). I mean, if someone's not just trying to bash on Python the argument doesn't make sense, because Python lets you use whatever whitespace scheme you want as long as it's consistent.
    – Delioth
    Commented Apr 30, 2019 at 14:05
  • 1
    Nice story but do you have references? how can we sure this is not just some kind of historical revisionism that fits your narrative? The first version of the Algol manual (web.eah-jena.de/~kleine/history) showcases indented blocks. One of the primary goal of Ada was to be readable: books.google.fr/… etc.
    – coredump
    Commented Apr 30, 2019 at 15:17
  • 3
    ...To be fair, I have had F# code refuse to compile before because I had an extra space at the beginning of one line. There weren't any lines on the same level around it. I had to stare at that for quite a while before it clicked what was wrong. :D
    – Kyralessa
    Commented Apr 30, 2019 at 15:18
7

Because some crazy people think its an "advanced feature"!!

Honestly it drives me mad. Not only do they remove the nice printable end of line and closure indicator characters ; { } THAT YOU CAN ACTUALLY SEE WITH YOUR EYES! But they don't even use tab for indentation!!! Apparently pressing space twice is better than a century of using the key SPECIFICALLY DESIGNED FOR THAT PURPOSE.

Fotunately most IDEs allow you to turn on 'show whitespace' so you can see the EOL character and all important multiples of space space that follow them.

I think they should all be forced to program in Fortran 77 untill they see reason

5
  • 5
    Q&A: Question and Anger! Commented May 13, 2019 at 8:25
  • 3
    You don't type the spaces or TABs in Python/Haskell, the IDE can indent for you if you type the : or where, \x ->, case of, etc...
    – aoeu256
    Commented Sep 4, 2019 at 13:19
  • 1
    1. You can see indentation with your eyes too. 2. Nobody who uses any kind of reasonable editor or IDE actually presses space multiple times to get indentation. Commented Feb 10, 2021 at 9:12
  • 1
    you cant see the difference between , , , or though
    – Ewan
    Commented Feb 10, 2021 at 12:55
  • 2
    when I started programming with python as my first language, I thought indentation was perfect and curly braces and semicolons archaic. Now its the other way around. I can't stand either how most functional languages including F# rely on indentation. What's an easy copy/paste in C# leads to constant problems in F# depending upon where I paste it and how the whitespace comes out. Not to mention difficult to debug errors because of some hard to spot whitespace
    – user4779
    Commented Mar 25, 2023 at 3:30
0

You are right about the possible errors. It is inevitable, though, as is the case with many other peculiarities of any language. Forget one curly brace in any C language, or Java, and you'll have to go through hell and back at times just to figure out WHICH curly is missing - especially true when you are using an environment that auto-indents as you go (tabs will give you the illusion that curlies line up when they do not). Languages such as Python (super hot right now) will make full use of indentation, and while cross operating system files are interpreted differently sometimes, I have never run into the problem of white-space misinterpretation with .py (python) files. That being said, I am a regular user of MacOS, Ubuntu and Windows 10. My guess is that they have formatting configured in such a way that would not mess up python files?

3
  • 2
    Forget one curly brace in any C language, or Java, and you'll have to go through hell and back at times just to figure out WHICH curly is missing -- Only if you fail to observe proper brace indentation and alignment. Allman Style, FTW. Commented Apr 30, 2019 at 17:03
  • 4
    and you miss the big red highlight saying missing brace
    – Ewan
    Commented May 1, 2019 at 6:10
  • 1
    What about the big red highlight saying your mixing TABS and SPACES?
    – aoeu256
    Commented Sep 4, 2019 at 13:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.