I had a coworker who kept making this common mistake with his async/await code.
var states = await GetAllStates(); var addressTypes = await FetchAddressTypes(); var phoneTypes = await FindPhoneTypes();
This code doesn't get the three data types in parallel like he wanted. It gets them in sequence, each waiting on the first to finish before starting. Moreover, the correct code seemed messy for no reason.
var statesTask = GetAllStates(); var addressTypesTask = FetchAddressTypes(); var phoneTypesTask = FindPhoneTypes(); var states = await statesTask; var addressTypes = await addressTypesTask; var phoneTypes = await phoneTypesTask;
Indeed, this is just begging for a neatness-obsessed dev like myself to decide the three extra variables are pointless, and to optimize them away by turning this back into the first example.
And the trick is this: use the
-ing form of the verb.
var gettingAllStates = GetAllStates(); var fetchingAddressTypes = FetchAddressTypes(); var findingPhoneTypes = FindPhoneTypes(); var states = await gettingAllStates; var addressTypes = await fetchingAddressTypes; var phoneTypes = await findingPhoneTypes;
-ing form of verbs, when combined with "is" or "has" as a helping verb, produces the continuous tense. "I am getting the states." "He is fetching the address types." "She is finding the phones." It denotes an action that is in progress. And that's kind of the whole point of async/await, to start several tasks/promises in parallel and only start waiting on their completion when you must.
Since adopting this naming convention, my coworker has never committed this antipattern ever again.
Shocking, I know.