Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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

share|improve this question
1  
I wouln't say it's "much easier to cut and paste". It's only little easier. –  Kugel Nov 2 '10 at 1:32
    
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 '10 at 6:36
add comment

6 Answers

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.

share|improve this answer
3  
Incidentally: cooking.stackexchange.com –  Jon Purdy Nov 2 '10 at 4:09
    
Also, something that's a pro for some could be a con for others. –  Larry Coleman Nov 2 '10 at 13:02
3  
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 '10 at 5:19
1  
P.S. The word you’re actually looking for is rationalise‌​. Also, everyone has a tendency to rationalise, not just programmers. –  Timwi Nov 4 '10 at 5:22
2  
@Timwi - Rationalization is a defense mechanism the ego uses to defend itself against perceived threats, not a technique for choosing programming tools. What I'm referring to is that programmers have a tendency to be Thinking types in Jungian terms. One weakness of these types of people is their inability to consider non-logical options. Yes, there are programmers who aren't Thinking types and there are Thinking types outside of programming. It's just that I was trying to avoid this answer turning into a discourse on Jungian psychology so I phrased it in terms of programmers. –  Jason Baker Nov 4 '10 at 6:10
show 1 more comment

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

share|improve this answer
1  
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 Harding Nov 4 '10 at 5:34
5  
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 '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. –  Timwi Nov 7 '10 at 16:21
1  
@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 '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. –  Kevin Oct 3 '11 at 15:40
show 5 more comments

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

share|improve this answer
    
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. –  user16764 Mar 21 '12 at 2:38
add comment

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

share|improve this answer
    
    
Multi line expressions are supported in Python -- just put \ at the end of a line. –  dbkk Nov 4 '10 at 6:24
6  
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 name Nov 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. –  Inaimathi Nov 5 '10 at 3:15
add comment

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 "     ".
share|improve this answer
4  
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 Rudolph Oct 3 '11 at 11:38
    
Seriously, a conditional multiple pages long? –  Winston Ewert Aug 4 '12 at 22:07
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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