Bio and Publications

Tuesday, October 26, 2010

Python and the "Syntactic Whitespace Problem"

Check out this list of questions on Stack Overflow:
About 10% of these are really just complaints about Python's syntax. Almost every Stack Overflow question on Python's use of syntactic whitespace is really just a complaint.
Here's today's example: "Python without whitespace requirements".
Here's the money quote: "I could potentially be interested in learning Python but the whitespace restrictions are an absolute no-go for me."
Here's the reality.
Everyone Indents Correctly All The Time In All Languages.
Everyone. All the time. Always.
It's amazing how well, and how carefully people indent code. Not Python code.
All Code. XML. HTML. CSS. Java. C++. SQL. All Code.
Everyone indents. And they always indent correctly. It's truly amazing how well people indent. In particular, when the syntax doesn't require any indentation, they still indent beautifully.
Consider this snippet of C code.
if( a == 0 )
   printf( "a is zero" );
   r = 1;
else
   printf( "a is non-zero" );
   r = a % 2;
Over the last few decades, I've probably spent a complete man-year reading code like that and trying to figure out why it doesn't work. It's not easy to debug.
The indentation completely and accurately reflects the programmer's intention. Everyone gets the indentation right. All the time. In every language.
And people still complain about Python, even when they indent beautifully in other languages.

17 comments:

  1. When first learning python I found the use of whitespace for defining blocks odd too. What was especially troublesome was the notion of tabs vs. spaces for the whitespace. Once you get a decent python aware editor that problem is no longer a problem and you are left with a language that helps enforce readability. Maybe "get a good python aware editor" should be the first step to every tutorial.

    ReplyDelete
  2. "Every[one] Indents Correctly All The Time In All Languages."

    Oh, if only that were true. Look at pretty much any Javascript code that isn't part of a framework. For some reason, JS coders seem to treat mis-indentation as an obfuscation technique.

    ReplyDelete
  3. There are a couple of times the whitespace bites you in Python (and not in non-whitespace aware languages).

    One is pasting code. If you are at different levels of the block or the pasted code contains tabs instead of spaces it's annoying. Other languages don't have this.

    Automatic re-indent is not and error free as in other languages.

    This also makes Python a bad language for (html) templating. Python based template languages end up looking like ruby (with end statements).

    But ... people don't mention these, they only say that it's hard to use whitespace. Well the rule is easy (replace { with : and indent consistently), not sure what the holdup is since no one seems to mention what I've just said.

    ReplyDelete
  4. s/not and error free/not as error free/
    sorry

    ReplyDelete
  5. Is there a Python auto-formatter?

    Because I don't format my code any more. I let tools format it. And I don't fool with it. I used to spend a lot of time on indentation, but I ended up letting it go and I'm a lot more productive now. (C#, SQL)

    ReplyDelete
  6. Attempting to do templating with django templating language can be ugly and horrifying, but jinja2 fixes most of those problems.

    What I find fascinating is that HAML, a syntax for HTML and XML that represents structure using indentation and a syntax similar to CSS, is written in ruby. It's also an extremely popular ruby library. There's no popular alternative for python.

    The same is true for its' whitespace significant CSS syntax, SASS, but that's understandable... since it's a product that has no barrier to using it standalone other than a zealotry for having everything in your language of choice.

    I miss HAML working with python. :/

    ReplyDelete
  7. I loved the significant whitespace in Python from the first time I saw it.

    @matt harrison:
    Cut and paste will mess up a program more ways than whitespace. I very often regret doing it. If the same code is repeated often it should be refactored into a function.

    @cade:
    Many editors/IDEs have Python formatters. emacs comes to mind.

    ReplyDelete
  8. Whenever you respond to complaints, you should ask yourself whether the complaint is worth answering.

    I think people who make a reflexive judgement based on whitespace (or even any syntax, so long as the syntax is internally reflective) are likely to either be zealots for some other technology, irrational, or - most likely - very inexperienced with real-world development. In other words, they are people whose acceptance the Python community (or any community) does not need.

    Python's whitespace requirement is a trade-off like any other. I find it easy to work with and aesthetically pleasing. It does have some pitfalls (tabs vs. spaces when working with poorly configured editors; pasting a block at a different level of statement nesting than the level it was copied from), but those are just different trade-offs to other languages where e.g. you may have more superfluous syntax or less consistency in code layout (which makes debugging other people's code and co-operation harder).

    Martin

    ReplyDelete
  9. I believe you meant "Everyone indents ..." when you wrote "Every indents ...". Also, for the commenter who mentions missing HAML, lookup shpaml ...

    ReplyDelete
  10. I'm not 100% whether this actually might be your point about the C example, but: This C code will not compile.

    The indentation hides the fact that the "if" only refers to the next statement. The "else" therefore has no matching "if" and the compiler will throw an error. In those archaic languages you need braces {...} :-)

    ReplyDelete
  11. Ok, having a look at your post once more, I now *do* think that was exactly your point (people getting indentation right when not getting the braces right)...

    Sorry about that stupid comment :-)

    ReplyDelete
  12. "Everyone Indents"

    Oh man, you don't know non-experienced programmers. In the beginning, make them indent their code is like make an turtle climb a mountain: they don't do.

    Python solves this problem in a very elegant way, by throwing syntax errors when indentation aren't done right.

    For me, at least, whitespace is a Python's killer feature.

    ReplyDelete
  13. There are people who don't indent correctly. When I was a TA for Programming 101, there was a student in the class that indented Every. Single. Line. Differently. I had to reformat his code to see what the heck he was up to.

    He was the only one who did this, though.

    ReplyDelete
  14. Sorry, but you have no idea what you’re talking about. When writing in C#, I don’t indent correctly. I don’t bother, because I don’t have to. Visual Studio fixes all indentation for me. If I did what you said, I would end up with wrong programs, as demonstrated in your own example. Instead I rely on Visual Studio’s formatting to tell me what I *actually* wrote. Your example demonstrates how to end up with a wrong program that doesn’t mean what you think it does. I get the feedback that tells me that my code structure is *actually* what I mean it to be.

    ReplyDelete
  15. So good topic really i like any post talking about STD symptoms pictures and more , you shall search in Google and Wikipedia about that .... thanks a gain ,,,

    ReplyDelete
  16. "Consider this snippet of C code."

    I don't understand your example. Is it correctly indented or badly indented? What does it do and what should it do?

    ReplyDelete
    Replies
    1. Indeed. No one in their right mind would write C code (or code in any language with C-like syntax) that looks like that.

      This same example is repeated over and over on the web. It seems Python's goofy syntactic whitespace solves only a single problem -- a problem that no one has ever actually had.

      The problems Python's syntactic whitespace causes, however, are varied and numerous. The astonishing number of apologetic articles like this are a testament to that.

      I'm sure replies will follow with all the usual "rules" (PEP 8, only use spaces, use a special editor) to solve the problems that Python apologists refuse to acknowledge actually exist!

      Delete

Note: Only a member of this blog may post a comment.