News for hackers. Stuff that matters.
Re:Writing Well-Documented Code – Learn from Examples by nijave on Saturday September 04, 2021 @04:28PM (#28418798)
>The number of times I have been misled by an outdated comment

Imo a problem with code review and not leaving comments. Just like tests need changed during refactoring, documentation and comments should also be cleaned up as part of the change

I have found those as well but place the blame on the person who made the subsequent change and ignored them

Re:Writing Well-Documented Code – Learn from Examples by prpl on Saturday September 04, 2021 @06:19PM (#28419886)
There’s not many code review platforms that are great at showing you relevant comments in a diff because they are often in a much different part of the file, so I kind of think it’s hopeless in a large code base to catch this sort of thing in a code review
Re:Writing Well-Documented Code – Learn from Examples by tln on Saturday September 04, 2021 @07:39PM (#28420430)
I now use VS code for reviewing PRs and it's easy to spot and then touch up comments.

With GitHub, not so much.

Not sure why code base size would make it harder... Workflow? Code unit size? The larger the code base, the more comment discipline matters IMO

Re:Writing Well-Documented Code – Learn from Examples by prpl on Saturday September 04, 2021 @08:38PM (#28420726)
I was mostly thinking about how small changes can sometimes touch a larger quantity of files on moderate or large code bases, so a bit more things to wade through when doing a review, but point taken.
Re:Writing Well-Documented Code – Learn from Examples by pcmoney on Saturday September 04, 2021 @08:02PM (#28420561)
Comments are not inherently bad but they do have inherent short comings. Still a useful tool but one that should be reached for rarely and last.
Re:Writing Well-Documented Code – Learn from Examples by TheoLib on Saturday September 04, 2021 @08:41PM (#28420747)
Comments are "reached for rarely and last". Whatever shortcomings comments have pale in comparison to being faced with and trying to figure out undocumented or too sparsely commented code, which, in my long experience, has been an order of magnitude more prevalent than sufficiently documented/readable code. Heck, even a wrong comment is sometimes preferable to no comment if it at least gives me a clue as to what the purpose of that section of code was at some point in its evolution.
Re:Writing Well-Documented Code – Learn from Examples by prpl on Saturday September 04, 2021 @09:22PM (#28420913)
I personally strive for code that can be read as a sentence, especially in complicated sections of code.

This sometimes leads to weird intermediate variables which might look superfluous, e.g. intermediates like:

foo_has_at_least_1 = len(foo) >= 1

(not a great example, usually it’s a bit more semantics. than that) but later code using those intermediates reads as english, which can reduce cognitive load when reading the code and obviate comments, and preconditions are more readily verified.

It’s literate programming and I prefer that to long comment blocks like those in some of the examples. All that said, it can be hard to get coworkers into such a habit, especially those who feel brevity and abbreviation is a virtue.

Re:Writing Well-Documented Code – Learn from Examples by nijave on Sunday September 12, 2021 @07:22PM (#28506067)
The problem comes not with what the code does but why the code exists in the first place.

The only time I ever feel the need to leave comments is to explain code that doesn't appear to be needed or isn't obvious why it exists. Usually this comes from agreed upon technical debt, short notice requirements with tight deadlines, or some other form of tradeoff.

"We're doing a simple check and throwing an exception here since we have work to introduce a more comprehensive permission system in xyz epic"

or something like

"There's a complicated race condition in xyz so we're doing a simple sleep here since the proper fix involves introducing a distributed lock"

Sure those types of things are cases you want to avoid but unfortunately you occasionally end up there anyway and leaving a short explanation about a compromise is better than ambiguous code that appears to serve no purpose

Re:Writing Well-Documented Code – Learn from Examples by pcmoney on Sunday September 05, 2021 @01:33AM (#28421871)
I dont think that held in the examples or a lot of what I have seen in practice once comments become the norm. Most classes should not need a comment in them at all. Most methods should be self-explanatory. A comment detailing every line of code is a huge smell.
Re:Writing Well-Documented Code – Learn from Examples by oxfeed65261 on Saturday September 04, 2021 @05:39PM (#28419529)
…and on the reviewer(s) who accepted the code change even though the comments no longer match the code.
Re:Writing Well-Documented Code – Learn from Examples by Gh0stRAT on Saturday September 04, 2021 @07:52PM (#28420499)
I agree, but git and most code reviewing tools I've used only show a few lines of surrounding context so the reviewers might not even realize there's a comment further up that needs updating.
Re:Writing Well-Documented Code – Learn from Examples by msbarnett on Sunday September 05, 2021 @03:20PM (#28426951)
Generally speaking, for all but the smallest of changes you should never only review the diff, but pop open the actual file on that branch (GitHub makes this fairly easy, I can't speak to other tooling) and consider the change in the context of the surrounding code. Does it contradict comments? Needlessly duplicate code from elsewhere in the file instead of creating a single function to be used in both places? Shadow variables confusingly? Add yet another 5 lines to a 100 line function instead of leaving the code better than it found it?

You can't really judge any of this if you're just eyeballing a diff. Diffs are just the starting point for a review, and while tooling could make this easier, the full file is always just a git checkout away.