DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
What's in store for DevOps in 2023? Hear from the experts in our "DZone 2023 Preview: DevOps Edition" on Fri, Jan 27!
Save your seat
  1. DZone
  2. Data Engineering
  3. Data
  4. Thoughts on Commenting Asynchronous Functions in C#

Thoughts on Commenting Asynchronous Functions in C#

Toni Petrina user avatar by
Toni Petrina
·
Feb. 19, 13 · Interview
Like (0)
Save
Tweet
Share
3.76K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Function summary

Let’s consider the following function:

public static Task<String> DownloadStringTaskAsync(this WebClient webClient, Uri address)

The official documentation for the wrapped method WebClient.DownloadStringAsync is:

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. Task is 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.

Cancellable methods

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 theIProgress<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.

code style Task (computing) Download Documentation Object (computer science) Strings Blocks Data Types Trait (computer programming) Enterprise architecture planning

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Distributed Stateful Edge Platforms
  • Mr. Over, the Engineer [Comic]
  • Load Balancing Pattern
  • 5 Data Mesh Best Practices From 4 Data Leaders

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: