by conaclos · 3y ago · 0 comments
> if I were to design a [...] language, it would push me toward indentation-sensitivity.
> That’s a notable shift from my prior perspective, where
> I felt that whitespace-insensitivity was preferrable to indentation-sensitivity.
I'm starting to have the same change of perspective for a different reason: widespread use of formatters. One of the drawbacks of white-space sensitivity is the accidental mixing of distinct white-spaces (e.g., tabs and spaces). Using a formatter eliminates this problem because all white-spaces are normalized.
I’ve switched my perspectives on whitespace significance. It makes for shorter source code but comes with a signal to noise problem. If there’s a mistake in the indentation it’s harder to determine intent. Versus if you expect well formatted and indented code (I love auto formatters like cargo fmt), and syntax to declare intent, then your tools can make better guesses and suggestions than if you only have one signal.
The other problem I’ve found working with Python is I often want to copy code and paste into my REPL but it’s usually inside of another context so I get an indentation error.
While I like significant whitespace, I LOVE tooling that understands my intent and helps me fix it. I’ll trade slightly more verbosity if it means better tooling. Though obviously I’m quite biased.
We still use a loosely representation (text) for a semantically very rich data format (code). When you look at this objectively that's madness.
We should finally use proper editors for our semantically rich data format instead of "dumb" text editors. (Which are by now actually not so dumb, and which do again completely crazy things to re-extract the semantic information from a representation that inherently erased this information while we're used a loosely text representation).
We should imho eventually switch to "binary source code" and start using proper tools to edit this data.
What we do currently is like editing Excel sheets in a hex editor…
Binary source code (or at least something in the ballpark of XML tech) would also solve a lot of other problems: For example the rendering of the data on screen would be independent form the actual data on disc. This means bitching around "code style" and even partly syntax could finally end. The stuff on screen could be rendered with the help of some "style sheets". Everybody would get the preferred look & feel for their code. (We do this already partly with syntax highlighting; we "just" need to extend this concept). Also things like version control, dependencies, and such, could be build in. Proper indices for searching (and refactoring code) could be also added (things that our editors today compute anyway, but in some ad hoc and very language specific way). Compilation of such code would be faster as all the parsing wouldn't be needed (as it would happen already in the editor; actually, also what is already done today, only that we than again throw away this information for no reason, so the compiler "may" regenerate it over and over on every run, instead that this stuff ends up in a kind of database which could be even part of the "binary source code" format).
I guess some innovative editor features nobody even considered until now would be also enable this way.
It's time for progress!
(Most likely, as almost always, someone will come up with some references to very old systems that did already this because they were ahead of their time; let's see what new stuff I'm going to learn this time :-D).
A lot of the ideas you're pitching have been pitched before and died for the same reason: parsing text is easy for humans and computers. Although I'd quibble with some minor points, like "compilation of such code would be faster". The major bottlenecks in a compiler are codegen, optimization, and linking compilation units. Even bad parsers are extremely fast.
Linters and auto-formatters didn't solve anything. They are just a constant PITA, and a source of arguments.
And to quibble a little bit: I said "faster". I did not say how much… Just parsing is indeed very cheep by now.
But a major performance issue was unmentioned: Type checking. In languages with advanced type systems that's the most costly part. Putting all that information into a database and carry it along "the code" would make things significantly faster. It would be like there would be only incremental compilation of the few line you're just editing.
Here's one: Alice Pascal
> In a syntax directed editor, you edit a program, not a piece of text. The editor works directly on the program as a tree -- matching the syntax trees by which the language is structured. The units you work with are not lines and characters but terms, expressions, statements and blocks.
Can you elucidate?
I'm assuming OP talks about saving something like that format directly, instead of text. Then information like 'this method is being called with these two parameters' isn't mixed with presentational info like whether you separated the parameters with newlines, or whether you use tabs or spaces. The downside being that it would not necessarily be immediately readable, the text editors would have to parse it and present it in a readable format, kinda like what happens now with, say, excel sheets.
Using Unicode for that is like pocking around in hex when compared to what we do actual with code in an IDE.
Edit: I just see the sibling has a even better explanation.
I completely agree with all your points on significant whitespace for programming languages. Where I think it makes a lot more sense though is for structured static data. My quality of live would be worsened without the likes of Haml, Sass and YAML.
I enjoy using slim in my views though prefer scss to sass. I think for many of those tools either misindentations raise an error (like failing a yaml schema validation) or result in incorrect but observable behavior (content isn’t in a div as expected).
It really comes down to how the tool is supposed to be used and how failures are handled.
In general I think that’s the difference between a good experience and a great one: when we can have beautiful failures that help us learn.
This makes no sense.
If the arguments against significant whitespace are valid for executable code they're also valid for markup.
If it's fine to use significant whitespace for markup / data literals even the perceived drawbacks apply than it would be equally fine for executable code.
Im not saying that more syntax is always better, but sometimes that extra bit that seems needless is helpful to clarify to the program “yep I meant what I wrote”
If there's an indentation mistake in an indentation sensitive language, then there's a mistake. Why does intent matter? Intent is lost because there is a mistake.
> Intent is lost because there is a mistake.
The signal is reduced but not gone. In a language like Ruby I can tell you plus or minus a few lines exactly where the problem is.
I discovered something really surprising regarding whitespace awareness of others, TL;DR that many juniors fundamentally misunderstand "tabs vs spaces", thinking that "tab" means the tab key rather than the tab character.
As a follow up to migrating a bunch of repos and discovering every possible combination of "wrong" indentation... I asked what people's preferences were, my assumption that the cause of this mess was different people working on the same repo with differing preferences. Only to find that this was not the case at all, they were mostly from the same individual. And those individuals had a severe misunderstanding of what "tab" vs "spaces" meant. Essentially many people are either not aware of the tab character or not aware that the tab key does not necessarily insert a tab character due to auto indent features. So those people interpret "using tabs" as "using the tab keyboard key", getting confused at why anyone would be using the space bar.
I then realised that the source of this misunderstanding comes from auto-indent defaults in modern text editors. The automatic, per-line indentation type detection, combined with automatic space insertion and automatic navigation over spaces when using cursor keys makes tabs and spaces indistinguishable unless you either turn these features off or render invisibles. This becomes a problem when chunks of code are pasted from somewhere else with different indentation styles, since they act as seeds of an indentation type that start to propagate and compete with adjacent line indentations. The worst combinations are competing space-based indentation levels, e.g 2 vs 4 spaces, when these are adjacent the automatic indentation behaviour cannot figure out which is correct since 2*2 = 1*4, 4*4 = 2*8 etc etc. This can make it impossible to set e.g a closing bracket onto the correct indent level. Rather than realise the auto indent is insufficient, It seems that novices will just submit to the editor - thinking it is somehow broken.
So rather than enforce a tab/space/indentation level style guide: I said I didn't care what they preferred, so long as they turned on invisibles and made sure their code is self-consistent by manually selecting an indentation style/level. i.e I've ended up mandating specific text editor settings rather than a style guide.
https://www.theverge.com/22684730/students-file-folder-direc...
Let's face it: The new generation has no clue how computers work.
Not that this would be a big surprise. My grandma also didn't know how computers work. That's not some knowledge you get born with. You need to learn it.
But the new generation never learned about "real" computers. They're just consumers of user grade entertainment and communication devices, until they get in contact with "real" computers the first time in university or on the job. (So exactly like for the generation of the now grand parents).
> The automatic, per-line indentation type detection, combined with automatic space insertion and automatic navigation over spaces when using cursor keys […]
That's absolutely crazy shit just to simulate the behavior of tabs with spaces!
If we would just use tabs (like they were invented, namely for indentation) there wouldn't be any issue.
The editor vendors should stop all this craziness described above and we would be good. Nobody is printing their source code any more on a line printer! Which was the only reason ever to actually use spaces for indentation. It's incredible how bullshit form the 70' still causes issues today even the actual reason for that bullshit can be found at best with luck in a museum.
Also makes it hard to copy a snippet to run inside a REPL or whatever.
In general just a huge amount of pain, and the "good thing" about how it forces well formatted code I've never seen as an issue with autoformatters and linters in othet languages.
Than everything just works.
(For that to work even better the stupid feature of some editors to remove white space form all empty lines should be disabled. So pressing enter or going to the next empty line will always position the cursor on the correct indentation level. Than it's easy to paste code blocks everywhere as even empty lines are correctly indented).
That's exactly the same "clean up" you need to do if you did something wrong while copy & pasting code without the useless block delimiters.
The rest is made up as someone pointed out already: Exactly this story comes up every time but in reality it never happens. (Besides when someone tries hard to get it wrong by all means only to prove their made up point).
Formatters will no save you from the many, many, many cases where indentation is simply ambiguous.
Edit: that is, if you go towards only indentation like Python and YAML
IDEA with its powerful formatters frequently struggles to properly format either of them, resulting in invalid code.
So it's not ambiguous.
> IDEA with its powerful formatters frequently struggles to properly format either of them, resulting in invalid code.
IDEA (by now) does very stupid things while trying hard to be smart. Also it's full of bugs. I stopped to use it because of all those annoyances.
I've found an easier solution is to forbid tabs outside string literals. You can't do the same with spaces.
That's only true most of the time, not all of the time.
If you ever want to output source code programmatically, then indentation becomes a big pain all over again.
The challenge you are refuting is about building said tools. Just because existing tools have pushed through this challenge doesn't mean it's not both tech debt for those existing tools and a challenge for any new tool in the space.
I think the best argument imo is that whitespace has to be properly maintained per line which adds computational cost and statefulness.
Instead of write(“{“) it’s indent += 1 and write(“ “*indent) and the start of each line.
Decrement where you’d have a }
And maybe the right answer is to import a compiler's worth of tooling to output a bit of code. But to me that seems like needless overhead, since meaningful whitespace isn't really a positive to begin with.
That's why code gen should be done on the AST level.
Just use the right tool for the job.
Could you tell how to set this up in say VSCode or IntelliJ?
AFAIK it works only one way: You can format tabs with an arbitrary number of spaces. But you can't render spaces with anything else than spaces. How should this even work? You would need semantic code analysis to make it happen…
TAB if (foo) {
TAB TAB bar(spam,
TAB TAB ....eggs);
TAB }
[1] https://www.emacswiki.org/emacs/SmartTabsIn 2023, it is technologically possible for the IDE repo to use tab characters and the git repo to use spaces, or vice versa. Also, in 2023, I don't care anymore.
s/^spaces|^tabs/whatever_width_you_want/g
that's what every editor does when you set indentation width to N spaces or TAB width to Xin language
whitespace
doesn’t matter so
much!