Search code examples
c#.netasynchronoustechnical-debt

Writing new code in async but calling sync


I am writing some new code and would like to write it using async and await, but the calling code is not currently written in async. Is it right to write the new code in async and call it sync until the calling code supports async?

Or should I write the code sync and then convert it at a later date? And would it be considered technical debt?

public Result Execute( Paramerters parameters ) {
    return ExecuteAsync( parameters ).Result;
}

public Task<Result> ExecuteAsync( Paramerters parameters ) {
    ...
}

Execute is on an interface and is called from some other code that is not yet async. Is it correct to create the async version and call it from Execute until the code calling Execute is converted to async? My old code is written in .net 4.5.1 but is not yet converted to async.


Solution

  • http://blogs.msdn.com/b/pfxteam/archive/2012/04/13/10293638.aspx has some good points on both why to avoid this, and how to alleviate the problem if you can't.

    However,

    Or should I write the code sync and then convert it at a later date?

    That's likely easier, and does get away from the issue entirely.

    And would it be considered technical debt?

    By definition it is, though:

    1. You may need to have synchronous calls well into the future anyway, to support that case, so you will already have that technical debt, you're just dealing with it.

    2. You do have that technical debt already. You said, "but the calling code is not currently written in async". There, that's your debt, already there.

    3. If the sync and async versions mirror each other (very common) and you place the methods next to their twins, it can be easy to do most changes to each at the same time.