views:

377

answers:

11

C++ is my first language, and as such I'm used to whitespace being ignored. However, I've been toying around with Python, and I don't find it too hard to get used to the whitespace rules. It seems, however, that a lot of programmers on the Internet can't get past the whitespace rules. From what I've seen, peoples' C++ programs tend to be formatted very consistently with respect to whitespace (or else it's pretty hard to read), so why do some people have such a problem with whitespace-based languages like Python?

A: 
George Edison
that's actually not a valid example. For Python, inside of `(` and `)` you can have whatever white space you like.
Ross Rogers
Yea, you can do the same thing you did in the C sample code in Python.You write each param on its own line.
jcao219
Heh-heh. I never realized that. Thanks for pointing that out. **But** my first point is still valid.
George Edison
Even in your example, `{` and `}` are redundant data that clutter readability. All the code hierarchy information is encoded in the spacing that you've done. Most people read code blocks based on the indentation anyway. Such that accidentally omitted `{` and `}` can be hard bugs to find.
Ross Rogers
+11  A: 

Perhaps your C++ background (and thus who your peers are) is clouding your perception of this (ie selective sampling) but in my experience the reaction to Python's "white space is intent" meme is anywhere from ambivalent to they absolutely love it. The reason a lot of people love it is that it forces people to format their code.

I can't say I've ever met anyone who "hates" it because hating it is much like hating the idea of well-formatted code.

Edit: let me put this in some perspective.

In the Java world there are two main methods of packaging and deploying Web apps: Ant and Maven.

Ant is basically an XML-based Make facility that has tasks for the common things you do. It's a blank slate, which is powerful, but it also means you have to write a lot of common things yourself and every installation is free to do things slightly differently. All of this is well-intentioned but can make it hard to figure out someone's Ant scripts.

Maven is far more fully features. It has archetypes, which are basically project types. Depending on which archetype(s) you use, you won't have to write any tasks to start, stop, clean, build, etc but you will have a mandated directory structure, which is quite deep.

The advantage of that is if you've seen one Maven Web app you've seen them all. You know the commands. You know the structure. That's extremely useful.

But you have people who absolutely hate Maven and I think it comes down to this: they don't like giving up control, even when it's ultimately in their interest to do so. Also, you'll find a certain brand of person who thinks that their use case is a justifiable exception. You see this personality trait a lot. For example, I think an old Joel post mentioned a story where someone wanted to use "enter" to go from the username to password form fields even though the convention was that enter executed the default action (usually "OK") so they had to write a custom dialog class for Windows for this.

Basically some people just don't like being told what to do and others are completely obstinate in their belief that they're right even when all evidence points to the contrary.

This probably explains why some supposedly hate Python's white space: they don't like being told how to format their code. They like the freedom of C/C++.

cletus
+1 I've met programmers who hate Python's formatting, but these are the same people who love C and assembly and have never done any serious coding in either.
BlueRaja - Danny Pflughoeft
Indeed, forcing people to format their code is a good thing. Code formatting is very important as a project grows, nobody wants to track something down in a 10k LOC thingy when every other file has a different coding style, so Python makes beginners think more about this. Also since you're now using indentation for both blocks and style, you can get rid of those stupid curly braces which by the way(like most programming characters) are ways off on a German keyboard. And nobody can tell me that he's doing serious work without an Editor/IDE that doesn't feature auto indent/tabs to spaces conver.
Ivo Wetzel
"The reason a lot of people love it is that it forces people to format their code." This makes sense. I personally can't stand to see inconsistently-formatted C++ code, so having the language itself force it could be nice.
Maulrus
We hate it because it breaks when white space changes. When Make and cron were written, that was wrong, okay. It's still wrong. It'll be wrong in the year 1e9. Somebody's going to screw it up reformatting code, and most code diff tools will, sensibly treat different kinds of white spaces as equivalent.
Tim Williscroft
A: 

Because change is scary. And maybe, among certain developers, there are some faint memories of languages with capricious rules about whitespacing that were hard to remember and arbitrary, meant more for compiler convenience than expressiveness.

Most likely, not giving whitespace-significance a fair shake before dismissing it is the real reason. Ask someone to fix a bug in a reasonably complex but well-written Python program, then ask them to go fix a bug in a 20 year old system in C, VB or Cobol and ask them which they prefer.

As for me, I have as much trouble with whitespace in Python or Boo as I have with parentheses in Lisp. Which is to say, none.

JasonTrue
+9  A: 

Somepeoplethinkthatwhitespaceshouldneverhavesyntacticmeaning.Theywritelikethis.

Mark Byers
Interesting enough, I remember working with an obscure scripting language where whitespace /was/ ignored by the compiler.
MiffTheFox
Old versions of FORTRAN ignored whitespace (not sure which versions).
Tom Hawtin - tackline
Any IBM FORTRAN up to at least VSFORT - The code is column structured with code only in columns 7 thru 71 with columns 1 through 6 being for labels and 72 through 80 being for comments or line numbers.
Dave
I think whitespace should have no syntactic meaning, at least in languages that have explicit block delimiter syntax.
RCIX
+6  A: 

It violates the Principle of Least Astonishment, because we have it ingrained in ourselves (whether for good or bad) that whitespace Does Not Matter in a programming language. Whitespace is one of those issues that has been left up to personal style.

I still have bad memories back from being a student of learning the hard way that 8 spaces is not equivalent to a tab in a Makefile... Ah, the sleep I lost...

Ether
As this is perhaps the most fundamental Python rule, I find it very hard to believe anyone could be surprised by Python's whitespace choice in the midst of coding. In fact, Python **by far** follows the principle of least astonishment more than any other language in existence, bar none.
BlueRaja - Danny Pflughoeft
"In fact, Python by far follows the principle of least astonishment more than any other language in existence, bar none."What you've got there, sir, is a controversial opinion stated as fact. It really depends on how you came into programming.
temp2290
A: 

They will have to get used to it. Initially I had a problem my self trying to read some examples but after using language for some time I started liking it.

I believe it is a habit that people has to overcome.

Irfan Mulic
A: 

The only complaints I (also of C++ background) have heard about Python are from people who don't like using the "Replace Tabs with Space" option in their IDE.

Walt W
A: 

Some have developed habits (for example: deeply nested loops, unnecessarily large functions) that they perceive would be hard to support in a whitespace sensitive language. Some have developed an aesthetic dislike for hanging indents.

Matthew S
A: 

There are several different types of whitespace (spaces, tabs, weird unicode characters, carriage returns, line breaks, etc.), they aren't necessarily visually distinct, and languages and editors may treat them capriciously. This isn't an argument against well-designed whitespace semantics, but many people are against all forms of it simply because of the possibility of poor design.

tloflin
+2  A: 

The only valid reason I have come across is that refactoring using cut-and-paste (not copy) without refactoring tools (or syntax-aware cut-andpaste), can end up changing semantics if an easy mistake is made.

Tom Hawtin - tackline
A: 

Whitespace is silence. There is only one type of silence, and it does not have a shape.

Any blind programmer will tell you that whitespace is silence, and every sighted programmer will agree with them. According to programmers who are accustomed to languages such as C++, there is only one type of silence: that used to disambiguate tokens. It does not have a shape or a size or any other defining characteristics other than simple presence.

In a language such as Python, whitespace is still silent, but its silence is to meaning as it is in speech: like punctuation, it creates silences that are significant to how the language is read.

Even though I would prefer to be able to format my source in any creative way I choose, you cannot help but admire the elegance in syntactically significant whitespace when it is described in this manner.

Jon Purdy
Or apparently you can.
Jon Purdy