1532
you are viewing a single comment's thread
view the rest of the comments
[-] nosurprises@lemm.ee 13 points 1 year ago* (last edited 1 year ago)

Code is the documentation. Any other form of documentation requires additional maintenance and becomes obsolete quickly.

[-] EvilMonkeySlayer@kbin.social 19 points 1 year ago

"Code is the documentation"

Is the laziest excuse I've come across. Like, not everyone knows everything and complex code exists, it is not hard to put in some text explaining the basics of what something does.

Like, unless you're dealing with that code everyday it's going to look like double dutch to anyone new coming to it.

I've had people say that to me before, and it is absolutely infuriating in its laziness and cop out excuse.

[-] lukas@lemmy.haigner.me 1 points 1 year ago

If someone must explain to me what code does in comments, then I think that code is bad.

Developers invented high-level programming languages for a reason. If someone writes bottom-top, unreadable code with comments, then they might as well write top-bottom, readable code without comments instead. That's what I believe self-documenting code is.

But if someone explains to me why code does what it does in comments, then that's fine. Essential complexity exists. The point is that that code is readable already, we know what it does, but not why. That's what needs an explanation in comments.

[-] Vlyn@lemmy.zip 12 points 1 year ago

The code shows what happens. But comments should explain why it was done this way.

Sometimes the code started simple and readable, till you ran into a weird edge case a year ago. Now the code no longer looks as obvious and another developer might scratch their head when they read over it. A small comment can help out there quite a bit.

Or you're doing something stupid in code not because you want to, but because management forced you to. So you put a comment there that the code isn't wrong, management wanted that behavior.

[-] magic_lobster_party@kbin.social 1 points 1 year ago

The why can usually be found in Git and issue trackers. Just look at the history of the file to get some decent clue about why a piece of code was programmed in a certain way.

[-] Vlyn@lemmy.zip 2 points 1 year ago

That works for newer projects, but on older projects there's a dozen commits for any given line and a handful of Jira tickets that have something to do with it, but none that say "Change exactly this". A comment why you made an interesting design decision costs a lot less time than trying to unwrap the commit history.. Especially when you can't even find a clue on why this was done as the commit might simply be "Implemented feature XY"

[-] magic_lobster_party@kbin.social 1 points 1 year ago* (last edited 1 year ago)

I think comments should be a last resort when all other methods are inadequate. Proper git commit logs have helped me many times to understand why a certain thing is needed.

Git logs don’t need maintenance either. They always tell what was true when the code was written. Comments need maintenance, and are often difficult to keep updated.

You can write a detailed comment about the whys of a particular function, only for it to become outdated in weeks when the functionality has changed.

[-] Vlyn@lemmy.zip 2 points 1 year ago

I still haven't found a proper command or tool to do a multi-commit git blame.

Like I want to know who changed the logic in this line. But the last commit was a format refactor. And the commit before that just changed a tiny detail. So now I'm digging through the entire file history just to find the spot where this one line was introduced or actually changed.

If you have any tips for that, I'm all ears.

[-] magic_lobster_party@kbin.social 1 points 1 year ago

I usually use the GUI in IntelliJ for those kind of purposes, but any other GUI might do. Then I perform a manual binary search to find the commit I want.

[-] saltesc@lemmy.world 9 points 1 year ago* (last edited 1 year ago)

Ah, I see you don't optimise enough.

It should be...

Past you "I've shed so many lines and boosted processing time. It's a masterpiece. I should document this for future me because it's complex as hell."

Future you "What. The. Hell..." five minutes later "Oh, I see. I'm a fucking narcissist trying to be god of something 10 people run once a week."

[-] vox@sopuli.xyz 5 points 1 year ago

all functions should at least have doc comments on them

[-] magic_lobster_party@kbin.social 1 points 1 year ago

No. Just give the function a good descriptive name with good descriptive parameters. Keep the function simple too. If you can’t, try to refactor and see if that helps.

If you are still unable to express yourself via code, then you should use comments to guide the reader.

[-] auchschonda@feddit.de 2 points 1 year ago* (last edited 1 year ago)

What about exceptions raised within the function? Will you also put them in the descriptive function name? ;)

[-] magic_lobster_party@kbin.social 0 points 1 year ago

You can find them by reading the code. It’s not difficult if they’re placed at proper locations.

[-] auchschonda@feddit.de 2 points 1 year ago

But a major point of a function is that you not have to read its implementation details.

[-] magic_lobster_party@kbin.social 1 points 1 year ago

According to who? If I have access to the source code, which I often do, I’d rather just read the code. Chances are that if documentation exists, it’s no longer up to date.

[-] auchschonda@feddit.de 2 points 1 year ago

Modularity. Part of it is defining a proper Interface for using the hidden complexity.

Exceptions are only one example. Functions can have behavior, inner states, prior calling requirements etc.. you cannot read from its mere prototype.

Do you really want everyone to read the inner code to learn that?

Chances are that if documentation exists, it’s no longer up to date.

This risk also applies to descriptive function names. They can be poor, wrong or outdated, too.

[-] wols@lemm.ee 2 points 1 year ago

This works as a general guideline, but sometimes you aren't able to write the code in a way that truly self-documents.
If you come back to a function after a month and need half an hour to understand it, you should probably add some comments explaining what was done and why it was done that way (in addition to considering if you should perhaps rewrite it entirely).
If your code is going to be used by third parties, you almost always need more documentation than the raw code.

Yes documentation can become obsolete. So constrain its use to cases where it actually adds clarity and commit to keeping it up to date with the evolving code.

this post was submitted on 02 Oct 2023
1532 points (98.4% liked)

Programmer Humor

32171 readers
612 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS