With one major exception, I’m one of those “if your code needs documentation, it’s not written right” people. The exception is code that answers the question “Why do that instead of this other obvious thing?” If I’m answering a “why” question with a comment, I always wonder whether the comment should go in the code or in the tests.
As an example, consider an app I’m working on. In response to a user request (picking a menu item), a potentially long-running network transfer starts. What the user sees while that’s going on is that the app switches to the tab that normally displays the table of data she’s asked for, that table has been erased, and there’s a progress indicator spinning there.
Getting that to work is more complicated than you might think. Let’s pretend you’re trying to understand it.
The code behind the menu item posts a notification:
The DirectoryController
’s initialization declared that this method gets that notification:
The RemoteDirectory
also fields the notification. It looks like this:
First question: do you think the comment is helpful? If not, should I also explain that the Savon SOAP library doesn’t (seem to) allow asynchronous operation? Should I explain that RubyCocoa doesn’t allow threading, so I can’t invent asynchrony myself?
Second question: Would you rather see the comment here, in the code, or in the tests? Here’s the test of the load
method:
Does the test name reduce or eliminate the need for the comment? (I would normally be using shoulda and assert2.0 here. I’m not because the project was originally in MacRuby, which didn’t handle them at the time I switched to RubyCocoa.)
If you think the name helps, does that suggest I should change the name in the product code to something other than load
? What?