Live data from Hacker News

Beware the Siren Song of Comments

leastastonished.com

21–30 of 34 posts

Re: Beware the Siren Song of Comments

#21

> The right place to document [performance] hacks is a commit message. This advice strikes me as patently insane. Serious performance hacks often involve subtle interactions among different parts of the code. When that happens, properly documenting them requires explaining what's going on in a line of code, for every line of code, right next to that line of code. Just sticking a wall of text somewhere else entirely a…

I've worked on teams that emphasize commit logs as a place to keep significant information about the code.

This inevitably coincides with a "minimize change deltas" mentality to development because else the commit history gets confused. Continuous refactoring goes out the window and so does the soundness of your code base.

The code should speak for itself, period. It should be clear and sound before and after each transformation it undergoes. And there should be no limit to what kinds of transformations it undergoes.

Re: Beware the Siren Song of Comments

#22

> The right place to document [performance] hacks is a commit message. This advice strikes me as patently insane. Serious performance hacks often involve subtle interactions among different parts of the code. When that happens, properly documenting them requires explaining what's going on in a line of code, for every line of code, right next to that line of code. Just sticking a wall of text somewhere else entirely a…

[deleted]

Re: Beware the Siren Song of Comments

#23
post #19

Am I the only one that doesn't find looking up commit messages as a followup to `git blame` as convenient as a code comment? When I see something like, 42 def wobble(self): 43 # Make sure the vent core has been frog-blasted, 44 # so that we know {rare bad things} won't happen 45 frog_blast(self.vent_core) 46 self.do_important_things() I find that a much faster answer to "Do I really need to foo the bar?" than if I ha…

The only place I have seen this kind of code history integration is Visual Studio 2013 Premium which requires Windows, TFS, and that you bought the Premium version!

Re: Beware the Siren Song of Comments

#24
post #19

Am I the only one that doesn't find looking up commit messages as a followup to `git blame` as convenient as a code comment? When I see something like, 42 def wobble(self): 43 # Make sure the vent core has been frog-blasted, 44 # so that we know {rare bad things} won't happen 45 frog_blast(self.vent_core) 46 self.do_important_things() I find that a much faster answer to "Do I really need to foo the bar?" than if I ha…

  Is this a matter of insufficient knowledge of my IDE?
After writing this, I realized that it a symptom of exactly that: I'm not fully savvy with PyCharm/IntelliJ, my Python IDE of choice. I'll run down the ways I know of to access blame history (in case others don't know it exists).

1) Git -> Annotate: This is almost exactly what I wanted!

This gives a `git blame`-like column next to each code line, and clicking an item will show a summary of the commit, including both the commit message and fields modified. I can right click one of the annotations and select "show diff", and can even see the modifications to any of the files in the commit if necessary.

Unfortunately, this depends on a commit message explaining why that change mattered, AND on it being the most recent changes to those lines. Moreover, it doesn't show the full history of changes for those lines, such as if Alice added the real change, and Bob just did some whitespace changes.

I can find such information (with effort) using `gitk` or other git log browsing tools. If the most recent change (shown with git-blame) doesn't have what I need, I basically need to look at all of the changes for this file, going back from that commit, and see if I can find the one with a message that explains why ${hackity-hack} is necessary.

2) Right click a file tab or in the editor, and select Git -> Show History

This is a faster way of browsing the commit messages, but doesn't give enough information for me to tell whether that change was the one that affected the lines I care about. If it showed the diff of what the commit changed in my file, it would be more informative. (This is what gitk shows, for example.)

As far as I can tell, neither of these options is as comprehensively informative as using `gitk myfile` and backtracking from the commit that `git-blame` (or PyCharm's Git->Annotate feature) lists as the most recent change to those lines.

If comments don't describe what Is Happening or What Should Happen, they should be changed. This is much harder than maintaining unit test coverage, but I think the information is still very valuable to have in code comments rather than solely in commit messages. (It is certainly valuable to have them in commit messages as well, of course, as that documents how you felt at the time.)

Re: Beware the Siren Song of Comments

#25
As someone who is currently debugging a codebase that was written 5+ years ago by persons long gone, please, comment wherever you think it would be appropriate, and then some more. There are only a few things more hellish than being able to read the code and know what it does, while not being able to understand why something was coded a certain way.

Re: Beware the Siren Song of Comments

#26

With respect to TODOs and FIXMEs in the comments, I use them to mark places I know I will need to go back. Before calling a piece of code done, I grep for TODO and FIXME to make sure I haven't left anything unfinished.

I agree. I use TODO indicators as a code smell for things I forgot to add before making my pull requests, but I also see them as valuable reminders for future needs.

Sometimes there's a quick-but-less-generalized way of solving something, and you know there's a more elegant way of doing it, but for now you need to get something working/fixed and don't want to make sweeping changes.

Your immediate change might be to add a way for users to see that X is finished. A useful TODO message would be something which explains what you're doing

  # Tell users when their Solve-My-Foo is done
  # (Currently done in an ad-hoc way)
  # TODO: Add a notification infrastructure so that 
  #    arbitrary components of the apps can present
  #    notifications of Important Things
  #    e.g.:
  #    - celery job is done
  #    - a thing you've subscribed to has changed
  #    - a report/download/etc is ready to be retrieved
Building such a system might be overkill right now, if this part of your system is the only one that uses it, but a TODO message helps me see (the next time I make changes to it, or try to extend it, or use it as a template for reimplementation elsewhere) that there's more than one place we need this feature, and so it might make sense to plan/do those more comprehensive changes.

Re: Beware the Siren Song of Comments

#27

I've found quite the opposite. Most developers hate comments because they represent extra work. That work manifests itself in the creation and upkeep of the code. We've found that addressing comments as part of our code review process made a big difference in keeping things up to date. Regarding the issue of expressiveness: I never find myself commenting about what is obvious in the code itself. Often I'm writing abo…

+1. Commenting code or config files with an issue number and a date turns out to be a lifesaver. Your future self will thank his past self (instead of cursing him as usual).

(I am a sysadmin, not a dev, but pretty much everything Operations writes counts as code for these purposes.)

Re: Beware the Siren Song of Comments

#28

This approach works well in only one situation: when your code isn't doing anything substantial in terms of data processing. For me, comments are best used at explaining _why this is happening_, not what is happening. By far one of the best examples of code commenting I've seen is the explanation of the L2ARC (the level 2 adaptive cache) in ZFS: http://fxr.watson.org/fxr/source/cddl/contrib/opensolaris/ut... . Most o…

The hard part IME is keeping your audience in mind.

Your future self? Your coworker who might be hired next month? Your past self who would have wished to read said comment?

Who is the audience for the comment?

Re: Beware the Siren Song of Comments

#29
Nice article, but one passage gave me the willies:

> If you’re writing a public API for a library that’s getting exported to the world, or to a bunch of developers at your company, it may be easiest to maintain the documentation for that API in code comments.

An important idea that I always try to keep in mind when coding is that the me of a few months from now might as well be a different person. All those things I understand so well today, will be no more obvious to me next year than they will for some other person.

So I think that if I follow a methodology that makes a distinction between code for me and code for others, then I'm Doing It Wrong.

Re: Beware the Siren Song of Comments

#30
I really dislike his notion of using the commit logs as the correct place to explain your code. First, it means that any time I need to find out why something is being done in code I need to leave the code and switch from my IDE to my source control. Second, most tools like blame will only show the most recent change. Minor fixes and changes happen all the time. What are the odds that the explanation I want will come up instead of something like "Fixed a typo". Third, what about code from other branches and external code drops?I may be using code from somewhere else and not have access to the full history of changes, just stable versions. Fourth, what happens when you change source control? If all of the comments are in the Git commit logs that'll be a big problem if the company switches to Perforce.

Instead of just making meaningful commit logs part of the code review process why not make "reading the comments" part of the code review process?

Post reply on HN