r/programming • • Sep 26 '20

Found these comments by a developer inside the Windows Media Player source code leaked with the WinXP files yesterday, sort of hilarious

https://pastebin.com/PTLeWhc2
5.0k Upvotes

397 comments sorted by

View all comments

Show parent comments

39

u/Mourningblade Sep 26 '20

The good form of this is:

  1. Code may not contain any comments describing what it is doing unless it is heavy low-level optimization. Short function documentation is fine (longer for core library routines).

  2. Your reviewer must be able to read the code and understand what it is doing.

  3. Your reviewer must be confident that they could understand and modify your code after being woken up at 2 AM after a night of drinking.

This encourages simple code that does what it says. It is difficult to program this way, but worth it.

As for WHY the code does what it does, that should be in both:

  1. Commit description. If complicated enough, reference a design document.

  2. Tests. If you're implementing business logic and it's not clear why one would want foo to happen when bar occurs, maintain a spec document that focuses on justifications. That "document" can be part of your acceptance testing, if it works for your company.

I work on a sizeable system that does the above strategy and it works well, even in onboarding new people. The code implements non-obvious business logic, but you can tell what the code is implementing, and you can read the business rules spec to understand why.

Unfortunately, some developers learn this strategy as:

  1. No comments. The code should be clear about what is doing.

  2. I can read my code just fine.

  3. I'm not sure what your problem is.

14

u/luchak Sep 26 '20

I can mostly get behind this, and I certainly prefer self-explanatory code to comments, but I’m pretty surprised by the statement that motivation/reasoning type comments don’t belong in the code. That information should definitely go in commit messages - but anything sufficiently important should also go in the code; otherwise you’re making future maintainers trawl through commit logs to extract the info back out. Plus they have to metaphorically patch those messages on top of each other to figure out what is current. Tests can also help, sure, but they should be focused on behavior and not implementation, and so don’t help to explain some categories of decisions.

“Why” comments in code are especially helpful when documenting small but important design decisions (below what would usually go in a design doc), obvious-seeming lines of attack that don’t work, and quirks in interaction with dependencies.

4

u/weirdwallace75 Sep 27 '20

I can mostly get behind this, and I certainly prefer self-explanatory code to comments, but I’m pretty surprised by the statement that motivation/reasoning type comments don’t belong in the code. That information should definitely go in commit messages - but anything sufficiently important should also go in the code; otherwise you’re making future maintainers trawl through commit logs to extract the info back out. Plus they have to metaphorically patch those messages on top of each other to figure out what is current.

Plus, you have to assume you'll change VCS at some point. Sure, your codebase shouldn't live that long... but it might. Sure, the current VCS might always remain viable... but it might not. It's impossible to know what's going to happen even in the next few years.

2

u/justfordc Sep 27 '20

Nothing like running blame on some hard to understand code and seeing 'Initial import into git'.

1

u/FeepingCreature Sep 27 '20

Nowadays, it's hard to imagine a VCS change where you can't import your commit history.

1

u/Mourningblade Sep 26 '20

Great reply, thank you.

I think we definitely have common ground about "what" comments.

As for "why", let's dig a bit.

Consider "interaction quirks" - if there's a service or library (that you can't modify) that has some interesting state, the best approach in my experience is to capture that in an interface, not to document it where you use it.

Let's say we're using a filesystem that for some strange reason makes it so that we have to create a file as a separate call before opening and writing to it.

You're better off having a library with (deliberately terrible names) "openExistingFile" and "createThenOpen". Documentation for that library IS a good place to have "why" explanations.

One thing I want to point out here is the difference between adding a comment to calling code saying "must create before open, see REF" and documentation for a facade class.

Similar to my "deep optimization" exception, really strange HTML templates that are not obvious also merit a "yes, it's strange because of X" - because that stuff is really hard to abstract away.

I'm not trying to convince you, but I'd suggest giving it a try for a bit and see what it changes for you.

8

u/[deleted] Sep 26 '20

The comments are not there to explain what the code is doing, they are there to explain why the code is doing it. There is no such thing as self documenting code.

1

u/Mead_Man Sep 27 '20

There is but many people just can't comprehend alternative structures that convey an intuitive sense of the "why". Great code can convey that information through naming and architectural choices. It takes a long time to do this on novel problems and most people have no sense that it is even possible, so it might feel like a golden goose for some.

1

u/[deleted] Sep 28 '20

How do you name "client said it should work like this despite not making any sense"?