2

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?

Alexei
  • 434
  • 4
  • 15
  • In which quora post? – max630 Apr 30 '19 at 07: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 Apr 30 '19 at 07:45
  • @Neil actually older languages such as fortran made use of syntactic whitespace. – Ewan Apr 30 '19 at 08:45
  • @max630 - sorry, I have fixed that. Thanks. – Alexei Apr 30 '19 at 08:47
  • @Ewan, sure, but like with almost every other feature of FORTRAN, it did it badly. Even Python does it badly to an extent as it allows tabs and spaces for those indents, which causes problems. F# gets it spot on by being appropriately opinionated and only allowing one of them. – David Arno Apr 30 '19 at 10:07
  • omg f# is terrible! have you tried the begin/end mode? – Ewan Apr 30 '19 at 10:11
  • 1
    This question is subject of a [meta post](https://softwareengineering.meta.stackexchange.com/questions/8930/example-of-incorrect-close-voting). – Doc Brown Apr 30 '19 at 10:40
  • "I remember a teacher saying that compiler do not care about whitespace" - well that's true for some languages, and not true about others. You might also remember a teacher saying that the liquid state is colourless. That's true for water but it's not true for orange juice. – user253751 Aug 20 '19 at 23:42

3 Answers3

16

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.

Glorfindel
  • 3,137
  • 6
  • 25
  • 33
Kilian Foth
  • 107,706
  • 45
  • 295
  • 310
  • 6
    sure requiring you to see the difference between 2 spaces and 3 spaces in a sea of whitespace is totally easier to read – Ewan Apr 30 '19 at 08:17
  • 6
    @Ewan What language requires that? – Kyralessa Apr 30 '19 at 08: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 Apr 30 '19 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 (http://web.eah-jena.de/~kleine/history/) showcases indented blocks. One of the primary goal of Ada was to be readable: https://books.google.fr/books?id=r6Fi2beH8xkC&pg=PA10&lpg=PA10&dq=ada+designed+for+readability+programming&source=bl&ots=zIbsKU3r7I&sig=ACfU3U0g0vqPMXMYh6dE6Mjyt65GQriLQQ&hl=fr&sa=X&ved=2ahUKEwiciKXpi_jhAhXJA2MBHQGSAOoQ6AEwB3oECAkQAQ#v=onepage&q=ada%20designed%20for%20readability%20programming&f=false etc. – coredump Apr 30 '19 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 Apr 30 '19 at 15:18
6

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

Ewan
  • 70,664
  • 5
  • 76
  • 161
  • 5
    Q&A: Question and Anger! – Brecht Machiels May 13 '19 at 08: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 Sep 04 '19 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. – Jon Bentley Feb 10 '21 at 09:12
  • you cant see the difference between , , , or though – Ewan Feb 10 '21 at 12:55
  • 1
    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 Mar 25 '23 at 03:30
1

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?

Adam S.
  • 19
  • 3
  • 1
    `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](https://en.wikipedia.org/wiki/Indentation_style#Allman_style). – Robert Harvey Apr 30 '19 at 17:03
  • 3
    and you miss the big red highlight saying missing brace – Ewan May 01 '19 at 06:10
  • What about the big red highlight saying your mixing TABS and SPACES? – aoeu256 Sep 04 '19 at 13:17