Why Indent Code?
9 comments
Your logic is sound, although I can't really imagine anyone adopting this scheme.
Actually, I would take it much further:
Instead of code being stored as a plain text file, store an abstract syntax tree in some binary form. Then all indentation, whitespace, keywords -- in fact, practically the entire syntax -- could be defined according to your preferences, while maintaining full interoperability with fellow developers.
Of course this would require an editor/IDE specially designed to work this way, and a VCS that can diff the binary AST in a syntax-agnostic manner.
This is what I'm currently working towards, as part of a programming language that is defined only by its semantics (the syntax itself is dynamic/extensible).
Check back on me in about 10 years :)
Actually, I would take it much further:
Instead of code being stored as a plain text file, store an abstract syntax tree in some binary form. Then all indentation, whitespace, keywords -- in fact, practically the entire syntax -- could be defined according to your preferences, while maintaining full interoperability with fellow developers.
Of course this would require an editor/IDE specially designed to work this way, and a VCS that can diff the binary AST in a syntax-agnostic manner.
This is what I'm currently working towards, as part of a programming language that is defined only by its semantics (the syntax itself is dynamic/extensible).
Check back on me in about 10 years :)
Most diffing algorithms used by code review apps support an ignore whitespace mode.
For example with Github https://github.blog/2018-05-01-ignore-white-space-in-code-re...
For example with Github https://github.blog/2018-05-01-ignore-white-space-in-code-re...
All you are proposing really is that code be stored in a common format that editors can parse and display. Tabs and spacing literally are already exactly that - a common format that text editors can properly display.
Otherwise, it would require text editors to have knowledge of how to parse a specific language or serialization, which requires a lot of extra that most wouldn't support.
Otherwise, it would require text editors to have knowledge of how to parse a specific language or serialization, which requires a lot of extra that most wouldn't support.
Most text editors have this understanding anyway. That's how they autoindent code or colour keywords and variables. Yeah I guess I'm just proposing a different common format but in the end I believe it to be slightly better since it solves a few problems.
No, I don't think this will ever happen and it would cause a lot of pain to transition to it. But I just think it's an interesting idea which could have worked had it been done like this from the beginning.
No, I don't think this will ever happen and it would cause a lot of pain to transition to it. But I just think it's an interesting idea which could have worked had it been done like this from the beginning.
What beginning? Back in the 1950s?
Use Go. It forces a consistent format.
Not sure what problem you propose solving.
Use Go. It forces a consistent format.
Not sure what problem you propose solving.
it’s a good idea in theory, if every programmer in the world adopted the methodology right now. practically though, if you tried to implement it, it’d just be added on top of all of the formatting strategies that already exist, adding more confusion and noise to the problem.
Relevant xkcd:
https://xkcd.com/927/
Relevant xkcd:
https://xkcd.com/927/
True, I'm not saying we should do it since not everybody would follow and it would just cause more pain. But if everybody would then I think it would work better than the current approach.
I still indent my code (obviously) and follow language style guides because if I everybody did what they thought was best every code base would be a complete mess.
I still indent my code (obviously) and follow language style guides because if I everybody did what they thought was best every code base would be a complete mess.
Why not set your editor to not indent, and use git diff -w to ignore whitespace differences? Change your habits rather than try to change the world.
Most programmers would find unindented code hard to read.
Most programmers would find unindented code hard to read.
Well that's the point really. Nobody would be looking at unindented code. The point of my post is that the only reason for indentation is visualisation purposes. It's not there to be read by the machine (except for in Python) and is not a description of what is being done.
Just as we leave other visualisation to the editor such as colouring variables, all I'm asking is why not follow the same approach when it comes to indentation?
Just as we leave other visualisation to the editor such as colouring variables, all I'm asking is why not follow the same approach when it comes to indentation?
But it leads to many annoying issues, say with git tracking line changes (if I un-indent a code block it is all marked as changed even though the logic behind it hasn't, so a reviewer shouldn't waste time looking over the changes). Also having to configure your editor to indent correctly and the creation of many indentation rules. It makes reaching the "80 character limit" come much faster depending on how you indent code and pushes code off over your screen so you have to use line wrap or scroll sideways.
I was thinking, why not instead of indenting the code we could not indent the code and leave it to the editor to handle formatting it visually.
Now any editor will be able to parse this and display it how I would like. Say I prefer 2 space indentation it can do that instead of 8 which others prefer. Maybe I don't like indenting the code and instead would like the first "indentation" level to be underscored green, the second blue and the third red. Well it would be up to me.
Is this a horrible idea? I think it would solve a few problems and doesn't cause many problems.