4

In the recent async/await pattern, the recommendation is to end method names with "Async", such as "GetAsync()".

Now let's say I'm using the old asynchronous pattern, i.e. the one with the Begin/End method pair and IAsyncResult. And I have a method that wraps a call to that method, such as:

public void SendAsync(byte[] data)
{
    this.stream.BeginWrite(...);
}

Also assume that the EndWrite method passed to BeginWrite is the same for all data, i.e. it would be a method in the same class.

In this case, is it wrong to name this method SendAsync() since it might be confused with the new async pattern?

8
  • 3
    I question the return type void here, you're creating fire and forget methods? How do you know when it has completed the write operation? Seems like a recipe for disaster to me. Commented Apr 25, 2014 at 8:41
  • Naming is probably subjective. But it is bad practice to create void async methods: theburningmonk.com/2012/10/c-beware-of-async-void-in-your-code Commented Apr 25, 2014 at 8:43
  • 1
    To be consistent with .Net framework conventions use BeginXXX and EndXXX naming.. Also your method doesn't adhere APM It should return IAsyncResult Commented Apr 25, 2014 at 8:44
  • It is just an example to illustrate the point. You could, for example, add an Action parameter that will run when the End... method is invoked. Commented Apr 25, 2014 at 8:50
  • 1
    I think the problem here is that the moniker Async on a method means that it has some kind of asynchronous notification mechanism when the operation it starts has completed. For me that means that in order for the method to be named Async, there must be some kind of callback mechanism. However, I would say no, don't name it like this. Either supply the standard IAsyncResult parameters and create BeginXXX and EndXXX, or use Task and call it Async, or find your own naming conventions for your different style. But this is just an opinion! Commented Apr 25, 2014 at 8:52

2 Answers 2

6

As new frameworks/patterns/techniques are discovered/created/implemented, new rules or advises appear.

The naming conventions are just a way to create a standard in order to make code more unified between developers; that doesn't mean you can't use the one you like the most or is more useful to you (unless you are ina company that enforces conventions).

From my POV, I wouldn't adopt the new convention in what is now your legacy code.

Also, I always prefer to use a naming convention similar to the framework I'm using so that it will be easier to any other developer working with me.

Check this (kind of old but still pretty good): http://msdn.microsoft.com/en-us/library/xzf533w0(v=vs.71).aspx

Sign up to request clarification or add additional context in comments.

1 Comment

+1 just for the bold sentence :)
0

I would follow the APM (Asynchronous Pattern Module) and name my methods with BeginXXX and EndXXX.

Also, make sure your methods implement the pattern properly by returning an IAsyncResult and not void as you are returning from this method.

APM: http://msdn.microsoft.com/en-us/library/ms228963(v=vs.110).aspx

1 Comment

As I said in the assumption, there is a method EndWrite() which already uses IAsyncResult - it is just not visible from the code that will use this class/method.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.