19

There seems to be a conflict over whether its better to use whitespace or tokens like brackets to indicate scope. I've seen many praise python's solution to the inconsistent indentation problem, but many disagree:

Any language that has whitespace as tokens needs to die.

posted later on the same answer:

I was sortof anti-whitespace-as-tokens, until I actually tried it. It probably helped that my personal white-space layout pretty much matches what everyone in python-land uses. Perhaps it's that I am a bit minimalist, but if you're going to indent anyways, why bother with the {}s?

I can see some clear arguments for each side:

using whitespace:

  • helps reduce inconsistent indentation in code
  • clears the screen by replace visible tokens with whitespace to serve the same purpose

using tokens:

  • much easier to cut and paste code to different levels (you don't have to fix the indentation)
  • more consistent. Some text editors display whitespace differently.
  • more popular currently.

Are there any points I missed? Which do you prefer? Any words of wisdom after having worked with one or the other for a long time?


PS. I hate it when languages don't use the same token for each control structure. VB is really annoying with its End If and End While statements, most other languages just use {}'s for everything. But maybe that's a topic for a different question...

5
  • 2
    I wouln't say it's "much easier to cut and paste". It's only little easier.
    – Kugel
    Nov 2, 2010 at 1:32
  • 1
    As long as you keep blocks of code small and organized, the tokens really shouldn't matter... by the way, love the 'holy-war' tag, lol
    – Scott
    Nov 4, 2010 at 6:36
  • "Some text editors display whitespace differently.": ?? "more popular currently.": Popularity is often not related to the quality of an idea.
    – Giorgio
    Jan 12, 2015 at 7:43
  • I love whitespace syntax, and I loved coding in CoffeeScript and Haskell (yeah I know its hard). I've coded a bunch in JavaScript and C, and I just can't go back to }. However, Lisp s-expressions when they are replaced with box divs is best though : )
    – aoeu256
    Sep 2, 2019 at 3:09
  • I think it makes it harder to spot errors if you have to rely on whitespace alone. For example: stackoverflow.com/questions/21205836/… The only difference between the OP's code and the answer is the extra space. Having a } to delimit which code is inside the for loop and which is not is a lot clearer. Jan 6, 2020 at 10:47

7 Answers 7

15

I think a lot of us programmers (myself included) have a tendency to "logicalize" every decision. That's fine, but not every question has a logical answer. For instance, I doubt that chefs post questions on chefoverflow (if such a thing exists) asking for the pros and cons of apple pie vs cherry pie. It's a question of which you like better.

With that in mind, I think the simplest answer is to say "Some people like braces, some people like whitespace" and leave it at that.

7
  • 6
    Incidentally: cooking.stackexchange.com
    – Jon Purdy
    Nov 2, 2010 at 4:09
  • Also, something that's a pro for some could be a con for others. Nov 2, 2010 at 13:02
  • Excellent point. Personally I think they work together, and as long as the sope is clear, I won't complain (much).
    – Michael K
    Nov 2, 2010 at 13:50
  • 11
    I don’t agree with your analogy. There are pretty objective reasons to believe that whitespace-dependent syntax hampers programmer productivity, even in the programmers who love it. It’s a bit like saying people just don’t like the taste of cyanide — everyone’s just “logicalising” when they say that it’s poisonous. No. It doesn’t matter how much you love it, it’s still gonna kill you.
    – Timwi
    Nov 4, 2010 at 5:19
  • 6
    P.S. The word you’re actually looking for is rationalise. Also, everyone has a tendency to rationalise, not just programmers.
    – Timwi
    Nov 4, 2010 at 5:22
13

At the risk of sounding like an utter fanboy, I think everyone who claims that whitespace “helps reduce inconsistent indentation in code” has never used Visual Studio. With a single command (I think the default shortcut is Ctrl+K,D), all the indentation is instantaneously consistent¹. Furthermore, when pasting code the indentation is instantaneously corrected without having to do anything at all, and the same is true when writing new code or when wrapping something in an if or some other block (reformat happens as the } is typed). Furthermore, pressing Enter after a completed statement always places the cursor at the correct indentation level for the next statement, even if the previous statement was indented further because of an if or similar, making it very difficult to accidentally think that a statement is still under an if when it isn’t.

The point I’m trying to make is not that Visual Studio is great. The point I’m trying to make is that the IDE can automate indentation-fixing (and other formatting issues), but only if the meaning of the program does not depend on its formatting. This gives the programmer a greater opportunity to focus on the actual programming task. A syntax like Python’s is counterproductive: it is not possible to write an IDE that can “fix” the indentation of Python code because the indentation itself specifies some of the semantics.


¹ (I know there is a special case which VS refuses to reformat, namely array literals that span multiple lines, but that’s beside the point.)

12
  • 10
    Python indentation does not need fixing because it does not get broken (without someone noticing!). It is impossible to write Purify (program that helps detect memory leaks) for C# either, does not mean C++ memory management is superior.
    – dbkk
    Nov 4, 2010 at 6:22
  • 2
    @Dean: Why do so many people think they need Resharper for everything? What you describe is already in Visual Studio without Resharper.
    – Timwi
    Nov 7, 2010 at 16:21
  • 2
    @dbkk: Your indentation is broken as soon as you add an if line anywhere. You then have to proceed to fix the indentation manually.
    – Timwi
    Nov 7, 2010 at 16:22
  • 2
    you probably haven't worked on a team where indentation was lazy, inconsistent, etc. and fixing it was discouraged because it makes code diffs impossible.
    – Kevin
    Oct 3, 2011 at 15:40
  • 2
    @Kevin: Correct, I haven’t, and frankly, I wouldn’t want to. I’d insist on fixing all of it in one go, which causes only one unreadable diff which only affects insignificant whitespace and fixes all future diffs. Anything else would be counterproductive and harmful to the project.
    – Timwi
    Oct 5, 2011 at 9:19
3

Personally I find I need the blank line introduced by having a token on it's own line to make my mind realise that the code is in another scope.

I hate it when in python people go:

if something
    do something
    do somethingelse

    cleanup
else
    do the other thing

while in token land I hate when people copy and paste and don't clean up the indentation,

if something
{
I have been too lazy
      {
            to clean up
            }
what I pasted
}

or don't use a separate line for the token.

if something {
    I find this confusing
}
else {
especially when combined with the previous 
do something
}

I can read all three, but they aren't as I expect and I have to expend effort to parse them and as Joel says when things don't work like you expect it makes you frustrated.

3

Pro: There are no indentation/curly-brace-placement holy wars in the Python world as far as I know. Making this decision on behalf of developers has probably saved some pain.

Con: Anonymous functions are limited to one line. Sounds ok, but I frequently find myself writing up multi-line lambdas in Scheme (mostly to feed to map or apply in exactly one place, so it wouldn't make sense to declare separately).

5
  • Worth a read: Everyone Indents Correctly All The Time In All Languages
    – Benjol
    Nov 4, 2010 at 5:40
  • Multi line expressions are supported in Python -- just put \ at the end of a line.
    – dbkk
    Nov 4, 2010 at 6:24
  • 8
    To be fair, the lack of multiline lambdas is a limitation of Python, not of whitespace in general. In my whitespace-delimited language, if you introduce a lambda and an indented block follows, the block is used as the lambda body. Nov 4, 2010 at 12:51
  • @Note - Ok, yes, fair. Python is just the only whitespace delimited language I have any experience with. @dbkk so you're saying that lambda n: a = n \\na.sort() \\na[0:3] wouldn't return a syntax error? The ` \ ` trick lets you spread your single-line lambda across multiple lines, but you still don't get more than one actual line.
    – Inaimathi
    Nov 5, 2010 at 3:15
  • Haskell, CoffeeScript use whitespace but don't have the single line lambda limitation.
    – aoeu256
    Sep 2, 2019 at 3:23
3

The problem I found with a whitespace language was with multi-page conditional expressions. Adding a line in the middle of page two, and getting off by one space in the middle of all mess, can drastically alter the logic of your code. And even if someone's code is "correct", trying to read it makes a terrible guessing game. Which "if" is this 'else' for? I can count braces much easier than I can count invisible blank characters. And the posting machinery on this site keeps smashing them into a single space.

IMHO "{{{{{" is more reliable than "     ".
2
  • 11
    If you use multi-page conditional expressions you have other problems. Just don’t do it. In fact, even with curly braces, the code becomes an unreadable mess. This is actually another benefit of whitespace indentation: it prevents you from writing such horrible code. Oct 3, 2011 at 11:38
  • 2
    Seriously, a conditional multiple pages long? Aug 4, 2012 at 22:07
3

{} adds redundancy. Too much redundancy is bad, too few is bad. And manualy entering it is bad, esp. if its hard to use it.

So in times of bad/no IDE, whitespace style could be slightly better. But with powerfull IDE, braces are better.

4
  • 1
    You make a good point about redundancy I think. I've been mulling over parser-recovery for compilers for a while (following Clang's very interesting attempts to issue Fix-Its for the code) and what really helps here is that there is redundancy. Therefore, I think that no redundancy, while great in a perfect world, is actually harmful for real world programming. When normally redundant sources of information disagree, then it may be that a typo/mistake was made; without redundancy, this potential error goes undetected! That being said, I prefer no braces ;) Oct 3, 2011 at 18:16
  • "Noise" was the word Erik Meijer used in his Haskell videos.
    – user16764
    Mar 21, 2012 at 2:38
  • What if you delete a block and you forget to delete the } or delete the { by mistake when doing the opposite? Redundancy goes against DRY IMO.
    – aoeu256
    Sep 2, 2019 at 3:26
  • @aoeu256 I agree, redundancy does go against dry, but I think being dry is a balancing act. After all, unit tests effectively do the same thing. If your code is working, unit tests are a form of redundancy; they are wet. Perhaps it's better because unit tests aren't part of the functional code, more along the lines of documentation, but it does point out that certain forms of redundancy in certain situations are good. How much to include is a balancing act. It can catch mistakes or make code more clear, but it also can create rigidity and clutter up the code.
    – Semimono
    Jul 16, 2022 at 21:59
2

I don't think it's really a versus.
Pythoneers often criticize braces programmers as if they would readily violate indentation because they can.
In fact I always used 100% consistent indentation even before knowing about Python and its indentation scoping.

The fact is that braces languages could also impose correct indentation in the compiler and we would have the best of both worlds. See? no versus at all.

Pythoneers would argue that braces are useless when indentation is part of the syntax, but they are not, braces improve readability. I tried programming Python and it's a very interesting language for me, but have you tried having if-elif chains of more than 2 or 3 indentation levels? they don't look so neat anymore.

PS: I wrote braces but in a more general way, I mean delimiter tokens. The opening brace can be seen as redundant, but a language could go like this (in fact I'm sure there are languages exactly like this but I don't know them well):

if condition:
    statement
    other_statement()
end

PS2: 2019, 4 years after this answer. I learned Python and totally got used to its semantic indentation and don't find it difficult to read at all. Especially with the aid of modern IDEs that draw vertical bars to aid in identifying indent blocks.

5
  • I like that syntax because it's a good compromise and avoids braces. But it's costly for one-line statements, where e.g. in C we can leave away the braces completely :(
    – Jo So
    Mar 30, 2018 at 17:06
  • instead of nested if...elif you can try using dictionaries, and/or, continue, return, nested functions...
    – aoeu256
    Sep 2, 2019 at 3:21
  • 1
    @aoeu256 totally not the point of the question and my answer either.
    – Petruza
    Sep 5, 2019 at 3:02
  • I think in general a powerful IDE makes indentation vs. brackets less important, as the tooling makes the downsides of both less severe. The most interesting case is when you don't have a "powerful IDE, when you're using something more akin to notepad.
    – Semimono
    Jul 17, 2022 at 23:41
  • If someone is using notepad to code in 2022 there are more problems to care
    – Petruza
    Jul 19, 2022 at 22:06

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