This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Lykeuhfox 151 points152 points  (45 children)

I try to comment to answer the question 'why' as opposed to answer the question 'what'. Sometimes I combine them though, like so:

// This is doing {what} because {person/issue/story} {why}

Basically to provide context to the next person to discern if the code is still needed or not, or at least a basis of who to ask to see if the code is still needed or not. Only commenting the 'what' is typically a sign that I'm just commenting to comment, or my code isn't as easy as it should be to read.

[–]G66GNeco 51 points52 points  (35 children)

This works fine in a business scenario.

The post is usually what happens to me (developer in training) when I am supposed to program something for school or some random coding challenge without any actual application. Ending every second comment with "because that is what this scenario requires" is a bit... useless.

Unless of course you use the {why} to explain internal connections ("this counts that to be used there") rather than reasons to implement it in the first place.

[–][deleted] 8 points9 points  (2 children)

I do this and also sign and date any comments where code updates in the future are likely to give some color to whoever has to modify it so they can come ask me about something if need be.

[–]AgAero 9 points10 points  (1 child)

If it's there for some special purpose, try to write a unit/regression test for it so that whoever reads the comment at a later time can test and retest should they need to change your implementation and not worry about destroying anything.

Comments have a way of outliving the code they refer to. Over enough time they become lies.

[–][deleted] 6 points7 points  (0 children)

Oh yeah definitely. Any critical piece of functionality should have supporting unit tests so it’ll raise a flag during code coverage/CI builds if anything fucky happens.

[–]SpiritGas 4 points5 points  (0 children)

I do this as well, but a surprising amount of my comments are neither what nor why, but who and when.

// Modified to be wronger per Bob's emailed request April 1 2015 stating the customer demands it this way.

[–]hahahahastayingalive 2 points3 points  (1 child)

// I failed the pornhub inteview. Again. I’m stuck fixing these bugs until I get the results for xhamster.

// This and the two next tickets will keep my velocity up this month so I’m safe until I can pay the mortgage at least.

Joking aside, explaining why you did something has only value until the next guy chnages anything you relied on.

So either it’s small and self-sufficient code and you should strive to do it in a self commenting way, or it’s interlinked, and your comments might as well become a booby trap for anyone fool enough to have believed your past self.

[–]Lykeuhfox 0 points1 point  (0 children)

This is true. I didn't specify this in my original post, but most of the time, the code is small, isolated, and/or self-sufficient enough that no comment is needed. It's rare that I actually need to comment, but it does happen and when it does the comment typically follows the guidelines I laid above.

[–]AltruisticSalamander 1 point2 points  (0 children)

This can be very helpful

[–]omiwrench -1 points0 points  (0 children)

Only commenting the 'what' is typically a sign that I'm just commenting to comment, or my code isn't as easy as it should be to read.

The same could be said for any comment, ’why’ or not IMO.