0

This question already has an answer here:

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?

marked as duplicate by Thomas Owens Apr 30 at 11:48

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • In which quora post? – max630 Apr 30 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 Apr 30 at 7:45
  • @Neil actually older languages such as fortran made use of syntactic whitespace. – Ewan Apr 30 at 8:45
  • @max630 - sorry, I have fixed that. Thanks. – Alexei Apr 30 at 8:47
  • 1
    This question is subject of a meta post. – Doc Brown Apr 30 at 10:40
9

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.

  • 2
    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 at 8:17
  • 2
    @Ewan What language requires that? – Kyralessa Apr 30 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 Apr 30 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 Apr 30 at 15:17
  • 2
    ...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 at 15:18
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?

  • 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. – Robert Harvey Apr 30 at 17:03
  • 1
    and you miss the big red highlight saying missing brace – Ewan May 1 at 6:10
0

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

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