Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the accepted pattern for naming methods that return Tasks?

APM uses BeginXXX/EndXX pairs and the Event-based Asynchronous Pattern (EAP) uses XXXAsync and XXXCompleted pairs, but I haven't seen anything standard on how to name methods that return a task.

I have been using XXXTask:

Data GetData() 
Task<Data> GetDataTask()

but was wondering if a more standard approach has developed

like image 409
Karg Avatar asked Jun 02 '10 22:06

Karg


People also ask

What is preferred naming convention?

“Naming convention is a set of rules for choosing the character sequence to be used for identifiers which denote variables, types, functions, and other entities in source code and documentation” — Wikipedia.

Should you capitalize method names?

Method names should always begin with a lower case character, and should not contain underscores.

What suffix should be applied to a method that returns task or task T >?

If a public method is Task-returning and is asynchronous in nature (as opposed to a method that is known to always execute synchronously to completion but still returns a Task for some reason), it should have an “Async” suffix. That's the guideline.


2 Answers

For C# 5.0 (with .NET 4.5), the naming convention is XXXAsync for task returning methods.

If there already exists a method with this naming (for instance, on the WebClient already has a DownloadDataAsync method that implements the EAP pattern), then the Task returning async method should be named XXXTaskAsync.

like image 56
Karg Avatar answered Oct 05 '22 23:10

Karg


I'd recommend using the patterns in the ParallelExtensionsExtras library since that's done by the same team that made the TPL in the first place :)

http://blogs.msdn.com/b/pfxteam/archive/2010/05/04/10007557.aspx

Their pattern seems to be the same as yours: [SyncAction]Task for the method that does SyncAction async via a Task (which is returned) - DownloadDataTask, SendTask, etc.

like image 44
James Manning Avatar answered Oct 06 '22 00:10

James Manning