← Back to context

Comment by eru

9 days ago

An important addendum: code can sometimes, with a bit of extra thinking of part of the reader, answer the 'why' question. But it's even harder for code to answer the 'why not' question. Ie what were other approaches that we tried and that didn't work? Or what business requirements preclude these other approaches.

> But it's even harder for code to answer the 'why not' question.

Great point. Well-placed documentation as to why an approach was not taken can be quite valuable.

For example, documenting that domain events are persisted in the same DB transaction as changes to corresponding entities and then picked up by a different workflow instead of being sent immediately after a commit.

I don't think this is enough to completely obsolete comments, but a good chunk of that information can be encoded in a VCS. It encodes all past approaches and also contains the reasoning and why not in annotation. You can also query this per line of your project.

  • Git history is incredible important, yes, but also limited.

    Practically, it only encodes information that made it into `main`, not what an author just mulled over in their head or just had a brief prototype for, or ran an unrelated toy simulation over.

    • In fairness to GP, they said VCS, not Git, even if they are somewhat synonomous today. Other VCSes did support graph histories.

      Still, "3rd dimension" code reasoning (backwards in time) has never been merged well with code editing.

      8 replies →

    • If you throw away commit messages, that is on you, it is not a limitation of Git. If I am cleaning up before merging, I'm maybe rephrasing things, but I am not throwing that information away. I regularly push branches under 'draft/...' or 'fail/...' to the central project repository.

      7 replies →

  • But why would you ever put that into your VCS as opposed to code comments?

    The VCS history has to be actively pulled up and reading through it is a slog, and history becomes exceptionally difficult to retrace in certain kinds of refactoring.

    In contrast, code comments are exactly what you need and no more, you can't accidentally miss them, and you don't have to do extra work to find them.

    I have never understood the idea of relying on code history instead of code comments. It seems like it's all downsides, zero upsides.

    • Because comments are a bad fit to encode the evolution of code. We implemented systems to do that for a reason.

      > The VCS history has to be actively pulled up and reading through it is a slog

      Yes, but it also allows to query history e.g. by function, which to me gets me to understand much faster than wading through the current state and trying to piece information together from the status quo and comments.

      > history becomes exceptionally difficult to retrace in certain kinds of refactoring.

      True, but these refactorings also make it more difficult to understand other properties of code that still refers to the architecture pre-refactoring.

      > I have never understood the idea of relying on code history instead of code comments. It seems like it's all downsides, zero upsides.

      Comments are inherently linear to the code, that is sometimes what you need, for complex behaviour, you rather want to comment things along another dimension, and that is what a VCS provides.

      What I write is this:

          /* This used to do X, but this causes Y and Z 
             and also conflicts with the FOO introduced 
             in 5d066d46a5541673d7059705ccaec8f086415102.
             Therefore it does now do BAR, 
             see c7124e6c1b247b5ec713c7fb8c53d1251f31a6af */

    • Both have their place. While I mostly agree with you, there's a clear example where git history is better: delete old or dead or unused code, rather than comment it out.