this post was submitted on 01 Sep 2023
337 points (96.2% liked)

Programming

17503 readers
7 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities [email protected]



founded 1 year ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 1 points 1 year ago (1 children)

There's no such thing as self documenting code, unless every method and variable name has the word "because" in it.

Anyone can read what the code does. The comments are there to answer why it does what it does the way it does.

Why is invariably lost to time, if it's not committed to a comment here and there.

[–] [email protected] 2 points 1 year ago (1 children)

This is a pretty ridiculous position to take but if you believe it then I'm glad you write the comments you do.

There is an argument that commenting on the lack of expected code is valuable for this reason, but it certainly isn't true in all situations.

[–] [email protected] 1 points 1 year ago (1 children)

We can agree on "not all situations". Often the answer to "why did we do it this way?" is blazingly obvious, and no one wants a comment.

But we all know that sometimes the "why" isn't obnoxious at all.

As far as I can tell, developers who do believe in self-documenting code either haven't learned the power of "why?", or they have a secret technique for encoding "why?" into their code structure.

If it's the second thing, I would be delighted to be brought in on it. (No sarcasm. Maybe I've missed a trick here.)

[–] [email protected] 0 points 1 year ago (1 children)

I'll answer in a couple of different ways.

  1. If I am writing library code my why is you have an end use and I don't care why you use it and you don't care why I wrote it. You only care about what my code does so you can achieve your why.

  2. If we are working on the same code we have different whys but the same what. Then your comment as to why isn't the same as mine which makes the comment incorrect.

  3. We are looking at a piece of code and you want to know how it works, because the stated what is wrong (bugs). This might be the "why" you are looking for, but I call this a "how". This is the case where self documenting code is most important. Code should tell a second programmer how the code achieves the what without needing an additional set of verbose comments. The great thing about code is that it is literally the instructions on the how. The problem is conveying the how to other programmers.

There are three kinds of how: self evident, complex how's requiring multiple levels of abstraction and lots of code and complex short how's that are not apparent.

The third is where most people get into trouble. Almost all of these cases of complexity can be solved with only a single layer of abstraction and achieve easily readable self documenting code. The problem for many cases is that they start as a one off and people are lousy at putting in the work on a one-off solution. Sometimes the added work of abstraction, and building a performant abstraction, makes a small task a large one. In these cases comments can make sense.

Sometimes these short, complex how's require specialists. Database queries, performant perl/functional queries, algorithmic operations, complex compile time optimized templates (or other language specific optimizations) and the like are some of the most common examples of these. This category of problem benefits most from a well defined interface with examples for use (which might be comments). The "how" of these are not as valuable for the average developer and often require specialist knowledge regardless of comments for understanding how they work. In these cases what they do is far more valuable than how or why.

[–] [email protected] 1 points 1 year ago

You've given a lot of consideration to modern recently created code. But the best modern recent code goes on to become someone's legacy nightmare. (The most fit and correct code survives long after anyone really wishes it would.)

In high quality legacy nightmare code "why" is lost, unless someone wrote it down.

I've been on both sides of that mystery. "Why didn't they just do X?"

  • Sometimes it was because X didn't exist yet, or wasn't matute enough.
  • Sometimes it was because X is fundamentally the wrong solution, in a very subtle way.

There's two ways to know the difference:

    1. Painful trial and error.
    1. A comment (or document) answering "why".

I prefer the second way, but I happily charge more for the first way.