Thoughts on Commenting Asynchronous Functions in C#
While I was wrapping up my asynchronous extension methods library, I realized that to be useful, all methods should have proper documentation. Since the number of extension methods wasn’t really large, I’d decided to add it. After all, how hard can it be? It is practically already properly documented since all I did was wrapping EAP pattern (MethodAsync function with MethodCompleted event) and the original functions were documented. But extracting proper commenting style that is both complete and consistent was actually harder you would expect it to be.
I will go through several items that needed to be commented with explanation why particular commenting style was used. If you are using some other style, please don’t hesitate from leaving a comment.
Let’s consider the following function:
public static Task<String> DownloadStringTaskAsync(this WebClient webClient, Uri address)
The official documentation for the wrapped method
Downloads the resource specified as a Uri. This method does not block the calling thread.
However, the documentation for
WebClient.DownloadStringTaskAsync is worded differently:
Downloads the resource as a String from the URI specified as an asynchronous operation using a task object.
Other than specifying that it won’t block the calling thread, you cannot find the trace of the word asynchronous in there at all. Also, the non-blocking aspect of the asynchronous functions should be omitted since this is a common trait for all asynchronous functions – we’ve invented them for this particular reason!
After giving it a thought, I’ve rewritten it to the following form (which is consistent throughout the library):
Asynchronously downloads the resource specified as a Uri.
The general rule is that the summary begins with “Asynchronously”. One could also use “Downloads … asynchronously” if the emphasis is on the action and not on the way of performing it.
The return type
While the summary is rather straightforward, documenting the return type is somewhat tricky. Here we have three possible scenarios:
void– asynchronous functions that don’t return anything are also called fire and forget. Luckily, we don’t have to comment anything here.
Task– even though it returns something, you do not have an actual result here.
Taskis used only for tracking whether or not the task has completed. Proposed documentation:
The task object representing the asynchronous operation.
Task<T>– This is the only return type that actually has any sort of result. Since we have a result here, we should comment on what it is. Here is an example:
The task object representing the asynchronous operation. The Result property on the task object returns a string result of the operation.
Some asynchronous methods can be cancelled via the cooperative cancellation support which requires us to properly document it in the function summary. I’ve settled on the following style (for the overload of the above mentioned function):
Asynchronously downloads the resource specified as a Uri, and monitors cancellation requests.
Progress reporting functions
Another trait of (some) asynchronous methods is reporting the current operation progress. This is done via the
IProgress<T> parameter. Similarly to the cancellable methods, I add another clause to the end of the summary:
Asynchronously downloads the resource specified as a Uri, and reports progress.
If a method supports both cancellation and progress reporting, simply add both sentences to the end of the function summary.
If you have any other particular style of commenting asynchronous methods, please leave a comment.