Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I feel like if #1 was done today, there is no way on god's green earth that whitespace would be used for code blocks.

It is far and away the most common footgun novices run into when I'm answering questions about why their code doesn't work.





My background is philosophy of language. I studied formal/mathematical logic in grad school. I was always embarrassed that I couldn't code, but the computer sciences classes were teaching languages that were inscrutable for someone even with my background, with syntax heavily focused on jargony math and technical concepts like object orientation (likely java at the time).

Around 2010, I was talking about this with friend about this failing of mine, and he said "you should try python, I've heard it is popular with non-math folks." So I bought a book, and as soon as I opened it, I could just read it. It took me a couple days of reading to wrap my head around object orientation, but on the functional side, I could have written fizz buzz like, maybe half an hour after opening the book.

Humans have logic pre-built into our brains, it's just that we use natural language as our syntax. Python cleverly used as much of the natural language syntax as was practicable to remove the barriers to entry for non-math majors. Whitespace is perfect example of a natural language syntax feature.


The whitespace thing is actually one of python's major flaws. That feature attaches syntactic meaning to non-printing characters. From a human standpoint, there're many examples of silence having some kind of meaning. From an engineering standpoint, that entire methodology is insane. Communication needs to be positive and deliberate.

Remember that Apple SSL bug "goto fail"? That was a whitespace bug, because even if the C feature predated python, everyone's eyes had been trained to slide right off that particularly crass shortcut as python was widespread by that point.


>Communication needs to be positive and deliberate.

I don't know what you mean by this.

>The whitespace thing is actually one of python's major flaws. That feature attaches syntactic meaning to non-printing characters. From a human standpoint, there're many examples of silence having some kind of meaning. From an engineering standpoint, that entire methodology is insane.

It's not non-printed characters, it's alignment. The period is a parallel for the semicolon in programming, to signal the end of a unit, but the whitespace in python is a parallel to the bullet point, or poetic stanza. Those both parallel to python in the form of atomic statements.

Most people's concern is the hanging indentation. Here, I would argue that we can effectively prove that hanging indentation is vastly more parallel to natural language than braces. Simply search for "handwritten recipes" and you will see that in a natural language assembly exercises -- effectively a real world parallel to programming -- human beings naturally default to hanging indentation when grouping sub-categories of items together.

https://duckduckgo.com/?q=handwritten+recipes&iar=images

Does this parallel to the jargony math you'll find in math books? No. But it trivially flows from human beings in the real world, and there is nothing formally incorrect in the syntax. Thus, we would likely find that layperson would intuitively understand the hanging indentation, where as braces as syntax is jargony, and must be learned.


> Here, I would argue that we can effectively prove that hanging indentation is vastly more parallel to natural language than braces.

I see where you're coming from. However, I would counter that in the use cases you've shown, as well as all of the use cases I've seen in the real world, there are important differences between them and Python.

1. Hanging indentation is most often used in things that are small in scope, where you can easily see both the start and the end of the list. Python blocks can grow without bound.

2. Hanging indentation in real life doesn't require perfect alignment. Python does.


I don't think it has anything to do with Python. I had plenty of 'if' errors in C++ caused by indenting the second line and not putting braces around it prior to Python. They were always painful to debug. I finally just _always_ put a brace around an 'if' block, regardless of whether it is one line or many, and I've never had that problem again. I think the problem is that C lets you omit the brace for one line; it should always require a brace.

Python executes like it reads, which seems like a positive feature to me. Makes errors like C's two-line if block impossible.


There's functionally little difference between spaces being non-printable characters, unless there are examples of text editors that do not render offset empty space when they're used.

Spaces vs tabs, sure, that's an argument.

But it doesn't seem reasonable to argue that {something that is visible in a text editor} is different than any other kind of character.

It's not like Python was using the bell ASCII or somesuch.


> It is far and away the most common footgun novices run into when I'm answering questions about why their code doesn't work.

It was, in my experience, before Python 3 clamped down on mixed spaces on tabs; before the `SyntaxError`s got better (for example the handling of `try` without `except`); and before VSCode got so popular (such that all the novices were using who-even-knows-what random editor and you had to figure out every time how they were actually producing the indentation, whether it does/can convert tabs to spaces).

And, oddly enough, before LLMs. Not so much because they explain anything all that well, but because lazy clueless people now get correctly indented code generated in-place rather than copying and pasting from Stack Overflow and not having any clue how to make the pasted code line up properly.

But now I far more often see people who are clueless enough that they can't distinguish the REPL from a command line ("why is `pip install ...` a syntax error?"), or are struggling with whatever is the latest attempt by the Python team to make Python easier to install and manage on Windows, or who seemingly can't wrap their head around the idea that Python has to know where on disk to look for the installed libraries (or why it won't work to just put everything in the system environment). And in terms of the language itself, probably the biggest stumbling blocks are things like command-query separation ("why can't I `foo.append(bar).append(baz)`?") and just using functions properly (which typically boils down to "why doesn't `return foo` let me refer to `foo` in the calling function?", but generally stated completely differently).


Whitespace cleanliness is only part of the problem. The much bigger issue is the fact that the ending of a block is invisible. This leads to several problems:

1. It becomes much more difficult to tell beginners where the end of a block is, because the ending of the block isn't something you can describe verbally. You have to point to it instead.

2. Students who might be prone to using the wrong number of "}" or "end" tokens to close a block will instead un-indent the wrong number of times. It doesn't prevent the kinds of mistakes that mismatched parens tend to cause.

3. When closing a block, the thing you're trying to align a prior block with might not be on screen any longer. I don't know why, but I've seen more off-by-one spacing gaffes after the close of a nested block than I ever expected to see.

That said, I don't doubt that Python arrived at the choice of whitespace blocks empirically. However, I highly suspect that the majority of the users learning how to program in the late 80's and early 90's were familiar with monospaced text, which was ever-present in most productivity applications of the day, and thus easier for them to reason about.

And to be clear, I don't really care that much about Python's whitespace blocks. I don't like them, but I'm at the age where I feel like quibbling over minor syntax gaffes is beneath me, and Python is my top "swiss army knife" language when I need a job done quickly. My only point is that I feel like whitespace blocks are a relic of the time when they were developed, and I don't think they are as beginner friendly as advertised.

EDIT: After stepping away from the post, I realized that in my perfect world, a beginner language would look a lot more like Lua than Python. Then I remembered how popular Roblox is, and how popular Garry's Mod was before that. Given the success of those platforms, there might be something to it.


#1 helps make Python maybe the most readable language which is probably more important than some minor hassles formatting it.



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: