News for hackers. Stuff that matters.
Re:Should I Comment My Code? The Case Against Code Comments by celticninja on Thursday October 06, 2016 @08:41AM (#12651905)
I'm a junior/trainee developer and I was advised that comments should explain why something is happening, not what is happening. The what should be self explanatory by reading the code, but knowing why something is being done is useful when you, or someone else, has to look at it again in the future.
Re:Should I Comment My Code? The Case Against Code Comments by ed312 on Thursday October 06, 2016 @08:44AM (#12651917)
This is the advice I give the junior devs I mentor. Write your code clearly and for other developers to read (yes that one-liner is clever, but refactor it so someone who just worked a 14 emergency deployment can understand it). Comments are for the why - quirky business rules, environment-specific issues, explaining an algorithm. Etc.
Re:Should I Comment My Code? The Case Against Code Comments by madeofpalk on Thursday October 06, 2016 @08:50AM (#12651961)
And a comment explaining why is especially useful when the code is a bit more dense and hard to follow - which is unavoidable at times.

IMHO, your default mode should be to not to comment because you should instead focus on 'code as comments' - the code should be easy enough to read that it explains itself. Occasionally (for the reasons you pointed out) you need to fall back to english prose to explain those 'whys'.

Re:Should I Comment My Code? The Case Against Code Comments by wangchow on Thursday October 06, 2016 @09:37AM (#12652302)
I think this is one of those things that is more about wisdom than anything else. You get a sense of what to comment over time and experience.

Typically following language best-practices is ideal (pydoc, godoc, Javadoc, etc) so you can get easily-generated documentation for free. Following the standards will make any custom APIs consistent with the standard library API docs.

The most important thing though is to ask for peer review on the code. When you are writing code (just like regular language) it will make sense to you regardless of comments or proper variable names. Have someone else read it and tell you what the confusing bits are. If there is no one to do that because you're a lone wolf, then step away from the work for a couple days and play golf or something. Then come back, read the code, and ask if it could be more clear. Then either change variable names or add comments for clarification.

Re:Should I Comment My Code? The Case Against Code Comments by Xylakant on Thursday October 06, 2016 @08:54AM (#12651984)
on top of that the why can extend as to why this specific implementation route was chosen. Some algorithms are less efficient in a general case, but may be much more suitable to the specific case at hand. Things that were tried and proven not to work even though they seem beneficial at first glance.
Re:Should I Comment My Code? The Case Against Code Comments by libc on Thursday October 06, 2016 @09:11AM (#12652115)
Some high level "What" comments are useful though even if just for yourself, especially for code that can't be easily broken down into modular units. Even if the code itself is very clean, it's still faster to read that one line comment which summarizes it succinctly in plain English.
Re:Should I Comment My Code? The Case Against Code Comments by hliyan on Thursday October 06, 2016 @09:05AM (#12652070)
I can't agree with this enough. My advice is: when the reason for doing something is not self-explanatory, add a brief comment.
Re:Should I Comment My Code? The Case Against Code Comments by crdoconnor on Thursday October 06, 2016 @09:08AM (#12652089)
This is the main reason. I also find a few other instances where it's useful:

* Calling confusing 3rd party APIs - sure, maybe all your code is self-explanatory but your code probably has to talk to somebody else's code and theirs won't necessarily be so self explanatory.

* Links to wiki pages and tickets.

* As a stepping stone to cleaning up technical debt you might want to explain what is going on before refactoring the code into something more self-explanatry.

* Adding context to an otherwise contextless module.

* If somebody asked a question in a pull request based upon the code (if the reviewer didn't understand, it often needs a bit more explanation).

Re:Should I Comment My Code? The Case Against Code Comments by wruza on Thursday October 06, 2016 @09:16AM (#12652149)
It is also sometimes useful to determine places that should be commented not only from structure perspective, but also from call-stack. You can save few minutes yourself by explaining how this innocent assignment influences behavior several levels down there and vice versa, and that it was planned.
Re:Should I Comment My Code? The Case Against Code Comments by aidos on Thursday October 06, 2016 @09:09AM (#12652091)
Let's all post a sample comment!

    # we leave the existing pattern alone if it's the same
    # this is a performance optimisation to save us rerunning the
    # searches and cc resolution again
Re:Should I Comment My Code? The Case Against Code Comments by zaphar on Thursday October 06, 2016 @09:32AM (#12652258)

    # NOTE(coderguy): Calling this method before calling
    # Init will result in a runtime error.
I use comments to make people aware of invariants the code expects that aren't necessarily obvious from reading that code.
Re:Should I Comment My Code? The Case Against Code Comments by tempodox on Thursday October 06, 2016 @09:41AM (#12652330)
If possible, document invariants as part of the code, not as a comment. Make it as hard as possible to misuse the thing. People not only don't read manuals, they also don't read comments. Which isn't to say you shouldn't comment your code.
Re:Should I Comment My Code? The Case Against Code Comments by aidos on Thursday October 06, 2016 @11:23AM (#12653234)
I wouldn't use a comment in that situation either - I don't think a comment hurts, but I'd rather the code failed in an obvious way if I tried to use it incorrectly.

My comments are generally along the lines of "so you're here because you need to change something? here's what you need to know", rather than "this is how you use this thing".

Re:Should I Comment My Code? The Case Against Code Comments by herbst on Thursday October 06, 2016 @08:45AM (#12651925)
I learned to code in a way that the variable and function names also would tell you the why. And otherwise the git log is your friend.

Tl:dr; i only do comments to excuse really stupid code.

Re:Should I Comment My Code? The Case Against Code Comments by hellofunk on Thursday October 06, 2016 @08:49AM (#12651951)
No one wants to look through a lengthy git log to find the reason something was done to a particular block of code!
Re:Should I Comment My Code? The Case Against Code Comments by mattferderer on Thursday October 06, 2016 @09:16AM (#12652151)
Visual Studio Pro & up has a great feature that puts a tiny snippet of text above each class & method that says the last author, how many authors & how many changes have been done on that specific class or method. You can hover over this text and see a box with a scroll bar that shows you the commits & comments that changed the class or method.

I'm not sure if any other IDE or editors have this but I really love this feature as a method of documentation. I'm not saying it is the best method or the only method one should use, just a useful method that I wish more IDEs & editors had.

Re:Should I Comment My Code? The Case Against Code Comments by herbst on Thursday October 06, 2016 @09:30AM (#12652239)
I assume every damn editor out there has a plugin that makes this a complete non issue. I spend most of my time in a terminal anyway and know my way around it, i prefer it by far to comments, also because of the metadata like dates and authors which rarely is found in comments.
Re:Should I Comment My Code? The Case Against Code Comments by michael_h on Thursday October 06, 2016 @09:43AM (#12652348)
Half of the time, when I look at the git blame for a certain line, I get: 'whitespace-only change'
Re:Should I Comment My Code? The Case Against Code Comments by almata on Thursday October 06, 2016 @08:52AM (#12651968)
Isn't a short comment a much quicker method to get why something has been done that way instead of having to check git log? Also, if you rely on git log for that, don't commit messages become unnecessarily long and detailed?
Re:Should I Comment My Code? The Case Against Code Comments by xapata on Thursday October 06, 2016 @11:52AM (#12653461)
Commits should be long and detailed, though the details for the commit log are for a different purpose. In the commit logs I want to know why the change was developed -- what issue it fixed, what feature request it implements. I want the code comments to explain the logic of a large chunk of code, at a higher level than just reading line by line.
Re:Should I Comment My Code? The Case Against Code Comments by herbst on Thursday October 06, 2016 @09:29AM (#12652228)
no, that rarely happens. There are plugins where you can run git blame within your text editor by marking text. It is like you have a comment but without making the code more messy as it only shows with a "click".

Another point is that people tend to write awful comments anyway.

Re:Should I Comment My Code? The Case Against Code Comments by wruza on Thursday October 06, 2016 @02:59PM (#12655081)
>Another point is that people tend to write awful comments anyway.

This. When we talk about stellar teams with stellar writers both in programming language and english, it seems nice to throw comment here and there. But when I look at what we do at work, I just... want to s{//.+$}{}, because comments there are A) misleading, B) senseless, C) grammatically awful, D) obsolete. Today almost everyone can be a programmer, but only few know how to explain things in short text at right place. You have to be writer to do that.

The best way to test a comment is to turn it into code and test it as code. Or, in more modern way, make a neural network that parses comments and check if these apply.

Re:Should I Comment My Code? The Case Against Code Comments by sehr on Thursday October 06, 2016 @08:49AM (#12651948)
Any examples? That sounds like it might get awkward when it comes to reuse.
Re:Should I Comment My Code? The Case Against Code Comments by herbst on Thursday October 06, 2016 @09:34AM (#12652282)
Given we are a small team and know each others typical structures. It also helps that i only did this in a MVC environment, so structure was given mostly.

I dont want to link to company applications honestly, and my personal ones are way to messy in terms of inline documentation anyway.

Re:Should I Comment My Code? The Case Against Code Comments by lallysingh on Thursday October 06, 2016 @08:58AM (#12652019)
Sounds like you learned how to code, but not how to engineer software.
Re:Should I Comment My Code? The Case Against Code Comments by herbst on Thursday October 06, 2016 @09:37AM (#12652305)
Thats a bit much to suspect based on a single sentence don't you think?

I am sure there are environments where this would be super messy, but if you work in small teams in a given structure always following best practice and basic refactoring. 99.9% of the code gets pretty obvious in its use.

Re:Should I Comment My Code? The Case Against Code Comments by Walkman on Thursday October 06, 2016 @09:16AM (#12652154)
It's interesting what you say, because I sometimes don't know when a particular comment should go into code or commit message!
Re:Should I Comment My Code? The Case Against Code Comments by herbst on Thursday October 06, 2016 @09:33AM (#12652269)
If you would work in the company i worked (e-gov in switzerland) comments are strongly discouraged (except as said if you do something stupid and you expect the reviewer to notice and give some inputs).

My rule is like: Make the code say what it does, if you have to explain why it does what it does do it in the commit message.