News for hackers. Stuff that matters.
Re:Avoid Else, Return Early (2013) by optimuspaul on Monday March 26, 2018 @09:23AM (#16678638)
> Same way comments are extra weight that should only be in public or algorithm/need to know areas.

Unless you are writing completely brilliant code your future self will hate you if you skimp on comments. Also self-documenting code is great, but intentions are not always clear to another person trying to figure out your code.

Re:Avoid Else, Return Early (2013) by fouc on Monday March 26, 2018 @09:31AM (#16678683)
Only "brilliant" code ends up needing comments.

Plain code organized into understandable methods (usually no more than half a page of code), with good naming for variables & method names reduces the need for comments.

It's also easier to scan/read code if there's a minimum of comments in the way.

Re:Avoid Else, Return Early (2013) by vanderZwan on Monday March 26, 2018 @09:39AM (#16678737)
There is a ton of code that only exists because of issues elsewhere in the code. This is the opposite of brilliant code: these are the dirty patchworks, the hacks glueing the whole thing together. Yet often these hacks are necessary, at least until some bug is fixed elsewhere.

Clear, self-documenting code is great but it can't capture that holistic insight into what the whole program is doing. It can't capture context, because the whole point of clean refactored code is to be as context-free as possible.

Re:Avoid Else, Return Early (2013) by scott_s on Monday March 26, 2018 @09:44AM (#16678773)
Completely agreed. Put another way: the sentiment that code documents itself is only possible in purely logical systems with no edge cases or surprising consequences.

As an example, our code base has a comment which refers to our internal issue tracker which itself refers to this HN comment: https://news.ycombinator.com/item?id=9048947

Re:Avoid Else, Return Early (2013) by dhimes on Monday March 26, 2018 @10:06AM (#16678964)
It's also only possible if the code is thought of as a text and is proofread/edited rigorously for reading. My process is 1) write the code quickly- add comments 2) revise for clarity and efficiency (usually modularity), remove comments as confidence allows. This is where I might get the variable and method names correct for self-documenting code. Usually it takes several passes. But most often, "good enough is good enough" and I leave comments in. Just in case.
Re:Avoid Else, Return Early (2013) by vanderZwan on Monday March 26, 2018 @12:14PM (#16679959)
> which itself refers to this HN comment

I was secretly hoping you actually meant your own comment, creating a recursive loop. But a surprise John Nagle is even better

Re:Avoid Else, Return Early (2013) by jakobegger on Monday March 26, 2018 @10:01AM (#16678927)
Also, any non-trivial software project is going to end up needing workarounds for bugs in other software. And you really need to document those. Because otherwise someone is going to remove that second refresh() statement, not knowing that this works around a bug in OS version xyz.
Re:Avoid Else, Return Early (2013) by vanderZwan on Tuesday March 27, 2018 @05:51AM (#16686798)
Basically, "IE8 fix"-comments
Re:Avoid Else, Return Early (2013) by RussianCow on Monday March 26, 2018 @05:01PM (#16682557)
> There is a ton of code that only exists because of issues elsewhere in the code.

Or sometimes, not even that: issues elsewhere in the system, possibly completely outside of your control (e.g. the language/framework/library you're using, the OS, business requirements, etc).

Re:Avoid Else, Return Early (2013) by Aaargh20318 on Monday March 26, 2018 @09:58AM (#16678899)
> Only "brilliant" code ends up needing comments.

Code shows what is being done. Comments should explain why it's being done.

Re:Avoid Else, Return Early (2013) by lackbeard on Monday March 26, 2018 @02:59PM (#16681489)
Code shows how it is being done. Comments should explain what is being done.
Re:Avoid Else, Return Early (2013) by jessaustin on Monday March 26, 2018 @03:53PM (#16681931)
In some cases it seems that the name of a function takes care of the "what".
Re:Avoid Else, Return Early (2013) by zarex on Monday March 26, 2018 @10:29AM (#16679148)
That's a really nice approach. Thanks for sharing, I might borrow it sometime!
Re:Avoid Else, Return Early (2013) by gknoy on Monday March 26, 2018 @01:13PM (#16680533)
> Only "brilliant" code ends up needing comments.

Often you have horrible complexity imposed on you from outside -- business rules you're implementing, etc, which are _not simple_. You can write code that encapsulates a lot of that, and even refactor it so you can see _what_ the code is doing pretty easily ... but it's often _very_ valuable to document in a code comment (docstring, JSdoc, etc) WHY it's like that.

Arguably, the comment is not to explain the code, in this case, but rather to explain the twisted bureaucratic logic you're having to implement, so maybe that still counts as "brilliant" code. ;)

Re:Avoid Else, Return Early (2013) by efficax on Monday March 26, 2018 @10:18AM (#16679053)
Sometimes things should be commented. Like, for example, every time I have to do pointer arithmetic, I make a comment explaining why, because pointer arithmetic is opaque and error prone.
Re:Avoid Else, Return Early (2013) by djsumdog on Monday March 26, 2018 @09:42AM (#16678763)
I agree with this. When dealing with a modern programming language, it's just easier to name your functions and variables in such as way that they're readable. It makes sense to document public functions using the language's documentation syntax, if you're writing an API.

I look back at a lot of my old code and even without comments, it's easy to follow the logic because I used sensible names and constructs.

Re:Avoid Else, Return Early (2013) by jochung on Monday March 26, 2018 @10:44AM (#16679239)
I have the exact opposite experience. If you find yourself annotating your variables and methods with extra adjectives and nouns, your code isn't simple enough. Those explanations go in code comments.

Often it means you can refactor it so that there is only one of any "thing" so there is no need to clarify which version or role this "fooBarThing" is serving to disambiguate it from the other "bazBarThing".

Functional composition and well structured data is the key to this. It's basically halfway to point-free style.

Re:Avoid Else, Return Early (2013) by fouc on Monday March 26, 2018 @10:30PM (#16684664)
The variable name can just be fooBar, and bazBar, and organized/namespaced such that you could have Thing.fooBar and Thing.bazBar (assuming you want those exposed to the rest of the application).

Readable variable names doesn't mean wordy names. I would avoid using more than 2 words in a variable name.

Re:Avoid Else, Return Early (2013) by khedoros1 on Monday March 26, 2018 @04:12PM (#16682093)
Good, clear code, with nice names and such shows what the code is doing and how it's doing it. It doesn't tell you anything about why it's being done, or the context of the function within the code.

You're lucky if you're dealing with code that's either small enough or simple enough not to need the added context...or if you only need to read your old code.

Re:Avoid Else, Return Early (2013) by phamilton on Monday March 26, 2018 @11:15AM (#16679497)
I find the better my types are, the less I need to explain. Canonical morphisms generally don't need comments.
Re:Avoid Else, Return Early (2013) by golergka on Monday March 26, 2018 @10:09AM (#16678991)
Sometimes requirements or quircks of third party library need a comment.
Re:Avoid Else, Return Early (2013) by efsavage on Monday March 26, 2018 @05:08PM (#16682605)
For glue/app/ux code that's mostly autocompleted, sure. But for domain-specific, algorithmic, magic-formula type stuff, comments are sanity made manifest.
Re:Avoid Else, Return Early (2013) by lackbeard on Monday March 26, 2018 @02:58PM (#16681479)
For an alternate perspective you might find persuasive, I recommend you read through the following slide deck: http://www.sqlite.org/talks/wroclaw-20090310.pdf. (Relevant part starts on slide 80, but I recommend you start from the beginning for full context.)
Re:Avoid Else, Return Early (2013) by Mithaldu on Monday March 26, 2018 @09:28AM (#16678663)
Comments are not documentation.

Documentation explains HOW to use a thing. Good comments explain WHY a thing is strange. Bad comments explain WHAT a thing does and must be made redundant by extracting and naming the thing.

Re:Avoid Else, Return Early (2013) by titzer on Monday March 26, 2018 @09:35AM (#16678707)
> Comments are not documentation.

Probably overly generalized to be pithy, but no.

Comments are by very definition documentation, which can and should cover of all of the what/why/who/how/where.

Documentation that occasionally explains how to use code can actually be useful!

Unless you actually get around to writing a user manual (which gets out of date), please do consider documenting how something should be used, particularly in libraries.

Re:Avoid Else, Return Early (2013) by Mithaldu on Monday March 26, 2018 @09:39AM (#16678741)
As a Perl developer i mean this literally and transfer it to other languages as well. POD (or a block comment above a function) describes the API and use cases of the function. Inline comments inside the function exist only to provide explanation for the next maintainer when they see a strange construct. Any other type of comment needs to be refactored.

Also, i never said documentation isn't useful. HOW and WHY are useful. WHAT in documentation and comments isn't because it should be in the variable/function/method/class/instance name.

(Who/Where/When are covered by the source repository and the blame function.)

Re:Avoid Else, Return Early (2013) by bgribble on Monday March 26, 2018 @09:34AM (#16678699)
IME it's the "completely brilliant" code that your future self will hate you for, and needs the most comments.

Moreover, when the weight of those comments trends towards 50% being justifications for why you did it that way, it's an excellent sign that your code has gone from "brilliant" to "super-genius", as in "Wile E. Coyote, Super-Genius".

"Completely dumb" code can be read by your future self without comments. Even, sometimes, by other people!

Re:Avoid Else, Return Early (2013) by krylon on Monday March 26, 2018 @09:49AM (#16678824)
Sometimes, I have seen (and, on at least one occasion, written) code that was brilliant in its simplicity. Very clever, but at the same time completely obvious. (In the sense that one needed to be clever to come up with it, but it was easy to understand if somebody showed it to you.)

But for the general case, I do wholeheartedly agree with you.

Re:Avoid Else, Return Early (2013) by zip1234 on Monday March 26, 2018 @09:35AM (#16678711)
The problem with this is maintaining comments. If you are using a compiled language or even linting, code that is part of the codebased is checked by a machine at least in a cursory way and the code that is there is what runs. The comments are not guaranteed in any way to pertain to the code that is in the repo and there is not a way for a computer to check the meaning of the comment to make sure that it was updated to match the code. Comments can get out of date, which may make them do the opposite of what they are intended for.
Re:Avoid Else, Return Early (2013) by nemo1618 on Monday March 26, 2018 @02:05PM (#16681025)
A while back I tried to create a commenting system that explicitly tied comments to blocks of code. The idea was that if you tried to change the code without also changing the comment, or vis versa, a git pre-commit hook would yell at you. Unfortunately it wound up being less tractable than I anticipated, but I think the basic idea has some promise.
Re:Avoid Else, Return Early (2013) by jasonpeacock on Monday March 26, 2018 @02:56PM (#16681465)
That's what code reviews are for, to help developers check each other for things that cannot be automatically linted.
Re:Avoid Else, Return Early (2013) by IshKebab on Monday March 26, 2018 @09:43AM (#16678768)
> The problem with this is maintaining comments.

That's like saying "the problem with healthcare is that it costs money". Of course comments can get out of date. The solution isn't to throw them out!

Re:Avoid Else, Return Early (2013) by YCode on Monday March 26, 2018 @09:50AM (#16678829)
"The problem with installing a roof on your house is one day it can be blown off."
Re:Avoid Else, Return Early (2013) by ajuc on Monday March 26, 2018 @09:49AM (#16678822)
There's no alternative for healthcare.

There's alternative to in-code comments answering the question "why" - it's commit messages. They can't be out of date.

Re:Avoid Else, Return Early (2013) by usrusr on Monday March 26, 2018 @11:18AM (#16679528)
But commit messages can easily get plowed under in some annoying "reformatted, I hate tabs/spaces" commit. I do like to see some "why" sprinkled in here and there that will survive those accidents. But I will nonetheless prefer blame output if it is still meaningful. Also, I enjoy a commit that removes redundant comments almost as much as one that removes redundant code.
Re:Avoid Else, Return Early (2013) by vk23 on Tuesday March 27, 2018 @11:08AM (#16689192)
You can either pass a commit id to git blame to see changes before the reformat happend or you can use git log like this:

git log -L0,10:file.txt

Re:Avoid Else, Return Early (2013) by TomMarius on Monday March 26, 2018 @03:52PM (#16681917)
What? So in my case I'm supposed to browse literally millions of commit messages in order to understand the code?
Re:Avoid Else, Return Early (2013) by ajuc on Monday March 26, 2018 @09:42AM (#16678764)
Comments that answer "what" are redundant. Choose your identifiers better.

Write good commit messages and comments that answer "why" are redundant too. Commit messages by their nature refer to the exact code that they refered to when they were written. Comments answering "why" after a few years are misleading anyway, because code changed around them.

Commenting public api etc is obvious, and most people do it.

Re:Avoid Else, Return Early (2013) by scott_s on Monday March 26, 2018 @09:57AM (#16678895)
When you read through a part of your system that you've either never seen before or have forgotten how it works, do you also read all commit messages for all of that code? I'm asking because that's the only way I can imagine one can learn about the edge cases and surprising consequences in non-trivial systems if you have a rule about not using comments to explain them.
Re:Avoid Else, Return Early (2013) by ajuc on Monday March 26, 2018 @10:05AM (#16678956)
No, I do a git blame, and see all the relevant commit messages.

If there's not 1 line of code from that commit remaining why is it relevant?

If something looks weird still - I go back to the commit that created this part of code and git blame that. I don't remember a case where I had to do 2 steps like that.

BTW we have a rule of putting JIRA ticket numbers in commits, that makes it even easier to find out. You can see the whole discussion that resulted in the code you try to understand, test cases that you don't want to break with your new changes, etc.

Re:Avoid Else, Return Early (2013) by gruez on Monday March 26, 2018 @11:14AM (#16679496)
>No, I do a git blame, and see all the relevant commit messages.

sounds great until there's a refactor (including moving code around), then all the "comments" get buried.

Re:Avoid Else, Return Early (2013) by macintux on Monday March 26, 2018 @11:35AM (#16679639)
It's a bit humorous that often the same people who insist the code should be enough documentation follow that up by clarifying that git, of course, supplies the rest.

So something entirely outside the codebase, which may or may not be available to the person who needs the information, and which may or may not need to be extensively searched through years of history to find the relevant commit message, which may or may not be sufficient to explain the code... is better than having a comment in the code.

Re:Avoid Else, Return Early (2013) by scott_s on Monday March 26, 2018 @11:36AM (#16679641)
Git blame is great. But having to look at all commit messages over, say, a few hundred lines of code just to know about standard pitfalls does not sound efficient. Edge cases are not just in the places where code is obviously doing something odd. Sometimes they hide in seemingly normal looking code - the kind of code you may not think it's worth diving into the commit history for.
Re:Avoid Else, Return Early (2013) by ropeadopepope on Monday March 26, 2018 @03:52PM (#16681915)
If I feel the need to add a comment to explain what I'm doing and why, then I usually take a step back and rethink what I'm doing and why. That urge tells me I'm trying to be too clever and that I need to simplify. I still add comments when I need them, but it's a rarity these days. Usually when I'm either on a time crunch or I can't think of a better way to do whatever it is I'm doing.
Re:Avoid Else, Return Early (2013) by seanwilson on Monday March 26, 2018 @05:18PM (#16682693)
> Unless you are writing completely brilliant code your future self will hate you if you skimp on comments.

The vast majority of comments I usually see can be rolled into variable names or function names. If you're writing lots of comments, that's a good indication your code is hard to understand, your variables are badly named and your functions are too long in my opinion. I think people that say "your code is bad if you don't have any comments" have things backwards personally.

Pretty much the only time I use comments is when I'm forced to write weird code to workaround an API bug, to explain an unintuitive optimisation or to give high-level architecture documentation.

I've honestly returned to code I've written myself maybe 5 years later and rarely had an issue that would have been helped with more comments.

Re:Avoid Else, Return Early (2013) by xg15 on Monday March 26, 2018 @03:54PM (#16681940)
I understand that as "document every class/module/public method with javadoc/pydoc/whatever and also explain the (few!) counter-intuitive bits of your code with inline comments. (You might find those deep within an algorithm, but usually not in plumbing or business logic code)."

That should leave you with a solid documentation of your application's components and APIs and a small amount of inline comments.

If you find you have more comments, either your code should be simplified or you're commenting trivialities.

As everything, it's a guideline with exceptions, not an absolute rule.

Re:Avoid Else, Return Early (2013) by imron on Monday March 26, 2018 @09:45AM (#16678796)
> your future self will hate you if you skimp on comments.

Likewise, your future self will hate you when comments fall out of sync with the code.

If the code is readable you don't need much in the way of comments.

Re:Avoid Else, Return Early (2013) by ModernMech on Monday March 26, 2018 @12:36PM (#16680153)
Code explains what the program does. Comments explain why a program does what it does. Code and comments are orthogonal, not coincident.
Re:Avoid Else, Return Early (2013) by gwbas1c on Monday March 26, 2018 @01:19PM (#16680607)
My rule of thumb is, if I have to ask a question during a code review, the answer needs to be in a code comment.
Re:Avoid Else, Return Early (2013) by z3t4 on Monday March 26, 2018 @04:55PM (#16682515)
Source control, eg the "comment" when committing code can also be used for comments.