
Why Indent Code? - keeyan
Indentation obviously makes code easier to read and parse (by humans). It is obviously very important.<p>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&#x27;t, so a reviewer shouldn&#x27;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 &quot;80 character limit&quot; 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.<p>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.<p>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&#x27;t like indenting the code and instead would like the first &quot;indentation&quot; level to be underscored green, the second blue and the third red. Well it would be up to me.<p>Is this a horrible idea? I think it would solve a few problems and doesn&#x27;t cause many problems.
======
dexwiz
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...](https://github.blog/2018-05-01-ignore-white-space-in-code-review/)

------
nartz
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.

~~~
keeyan
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.

~~~
gregjor
What beginning? Back in the 1950s?

Use Go. It forces a consistent format.

Not sure what problem you propose solving.

------
stevenhilder
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 :)

------
gregjor
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.

~~~
keeyan
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?

------
crimberlee
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/](https://xkcd.com/927/)

~~~
keeyan
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.

