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...

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise.
If this question can be reworded to fit the rules in the help center, please edit the question.

7 Answers
7

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.

Also, something that's a pro for some could be a con for others.
–
Larry ColemanNov 2 '10 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 KNov 2 '10 at 13:50

5

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.
–
TimwiNov 4 '10 at 5:19

3

P.S. The word you’re actually looking for is rationalise‌​. Also, everyone has a tendency to rationalise, not just programmers.
–
TimwiNov 4 '10 at 5:22

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.)

Great point. I use ReSharper, which fixes the indentation as soon as you type "}". You can configure it to "fix" your indentation in any way you like (e.g. K&R-style braces, different rules for anonymous classes, etc).
–
Dean HardingNov 4 '10 at 5:34

6

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.
–
dbkkNov 4 '10 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.
–
TimwiNov 7 '10 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.
–
TimwiNov 7 '10 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.
–
KevinOct 3 '11 at 15:40

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 ;)
–
Matthieu M.Oct 3 '11 at 18:16

"Noise" was the word Erik Meijer used in his Haskell videos.
–
user16764Mar 21 '12 at 2:38

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).

Multi line expressions are supported in Python -- just put \ at the end of a line.
–
dbkkNov 4 '10 at 6:24

7

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.
–
Note to self - think of a nameNov 4 '10 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.
–
InaimathiNov 5 '10 at 3:15

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.

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.
–
Konrad RudolphOct 3 '11 at 11:38

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):