Advantages vs Disadvantages: bool value in the parameter list to indicate async or not in C#
Here is a generic method for retrieval from an Azure Storage table (examples in the official doc).
public async Task<T> RetrieveOne<T>(string partitionKey, string rowKey, bool isAsync = false) where T : TableEntity
{
// To construct the query operation
TableOperation retrieveOperation = TableOperation.Retrieve<T>(partitionKey, rowKey);
// To execute the query
TableResult result;
if (isAsync)
{
result = await _table.ExecuteAsync(retrieveOperation);
}
else
{
result = _table.Execute(retrieveOperation);
}
// To parse the result
if (result.Result != null)
{
return (T)result.Result;
}
else
{
throw new Exception("The result retrieved is null");
}
}
I am using isAsync
in the parameter list to indicate whether this method is asynchronous or not. Did I make it?
The advantages are obvious: one can easily switch options between async
or not.
It seems that such a style would wrap the synchronous one(isAsync = false
) into a Task
as well, at the price of extra cost, since kicking out tasks alone costs time as well. Any other disadvantage?
c# asynchronous task
add a comment |
Here is a generic method for retrieval from an Azure Storage table (examples in the official doc).
public async Task<T> RetrieveOne<T>(string partitionKey, string rowKey, bool isAsync = false) where T : TableEntity
{
// To construct the query operation
TableOperation retrieveOperation = TableOperation.Retrieve<T>(partitionKey, rowKey);
// To execute the query
TableResult result;
if (isAsync)
{
result = await _table.ExecuteAsync(retrieveOperation);
}
else
{
result = _table.Execute(retrieveOperation);
}
// To parse the result
if (result.Result != null)
{
return (T)result.Result;
}
else
{
throw new Exception("The result retrieved is null");
}
}
I am using isAsync
in the parameter list to indicate whether this method is asynchronous or not. Did I make it?
The advantages are obvious: one can easily switch options between async
or not.
It seems that such a style would wrap the synchronous one(isAsync = false
) into a Task
as well, at the price of extra cost, since kicking out tasks alone costs time as well. Any other disadvantage?
c# asynchronous task
1
Do you observe such things in the NET framework? Or do you see things likeLoadData()
andLoadDataAsync
?
– None of the Above
Nov 14 '18 at 4:50
@Disaffected1070452 I just edited my question. Please refer to the examples added for your reference.
– Leon
Nov 14 '18 at 4:53
1
disadvantage inasync = false
you will not be able to use the result since it is not going to wait for execution.
– Just code
Nov 14 '18 at 4:56
add a comment |
Here is a generic method for retrieval from an Azure Storage table (examples in the official doc).
public async Task<T> RetrieveOne<T>(string partitionKey, string rowKey, bool isAsync = false) where T : TableEntity
{
// To construct the query operation
TableOperation retrieveOperation = TableOperation.Retrieve<T>(partitionKey, rowKey);
// To execute the query
TableResult result;
if (isAsync)
{
result = await _table.ExecuteAsync(retrieveOperation);
}
else
{
result = _table.Execute(retrieveOperation);
}
// To parse the result
if (result.Result != null)
{
return (T)result.Result;
}
else
{
throw new Exception("The result retrieved is null");
}
}
I am using isAsync
in the parameter list to indicate whether this method is asynchronous or not. Did I make it?
The advantages are obvious: one can easily switch options between async
or not.
It seems that such a style would wrap the synchronous one(isAsync = false
) into a Task
as well, at the price of extra cost, since kicking out tasks alone costs time as well. Any other disadvantage?
c# asynchronous task
Here is a generic method for retrieval from an Azure Storage table (examples in the official doc).
public async Task<T> RetrieveOne<T>(string partitionKey, string rowKey, bool isAsync = false) where T : TableEntity
{
// To construct the query operation
TableOperation retrieveOperation = TableOperation.Retrieve<T>(partitionKey, rowKey);
// To execute the query
TableResult result;
if (isAsync)
{
result = await _table.ExecuteAsync(retrieveOperation);
}
else
{
result = _table.Execute(retrieveOperation);
}
// To parse the result
if (result.Result != null)
{
return (T)result.Result;
}
else
{
throw new Exception("The result retrieved is null");
}
}
I am using isAsync
in the parameter list to indicate whether this method is asynchronous or not. Did I make it?
The advantages are obvious: one can easily switch options between async
or not.
It seems that such a style would wrap the synchronous one(isAsync = false
) into a Task
as well, at the price of extra cost, since kicking out tasks alone costs time as well. Any other disadvantage?
c# asynchronous task
c# asynchronous task
edited Nov 14 '18 at 4:52
Leon
asked Nov 14 '18 at 4:48
LeonLeon
15119
15119
1
Do you observe such things in the NET framework? Or do you see things likeLoadData()
andLoadDataAsync
?
– None of the Above
Nov 14 '18 at 4:50
@Disaffected1070452 I just edited my question. Please refer to the examples added for your reference.
– Leon
Nov 14 '18 at 4:53
1
disadvantage inasync = false
you will not be able to use the result since it is not going to wait for execution.
– Just code
Nov 14 '18 at 4:56
add a comment |
1
Do you observe such things in the NET framework? Or do you see things likeLoadData()
andLoadDataAsync
?
– None of the Above
Nov 14 '18 at 4:50
@Disaffected1070452 I just edited my question. Please refer to the examples added for your reference.
– Leon
Nov 14 '18 at 4:53
1
disadvantage inasync = false
you will not be able to use the result since it is not going to wait for execution.
– Just code
Nov 14 '18 at 4:56
1
1
Do you observe such things in the NET framework? Or do you see things like
LoadData()
and LoadDataAsync
?– None of the Above
Nov 14 '18 at 4:50
Do you observe such things in the NET framework? Or do you see things like
LoadData()
and LoadDataAsync
?– None of the Above
Nov 14 '18 at 4:50
@Disaffected1070452 I just edited my question. Please refer to the examples added for your reference.
– Leon
Nov 14 '18 at 4:53
@Disaffected1070452 I just edited my question. Please refer to the examples added for your reference.
– Leon
Nov 14 '18 at 4:53
1
1
disadvantage in
async = false
you will not be able to use the result since it is not going to wait for execution.– Just code
Nov 14 '18 at 4:56
disadvantage in
async = false
you will not be able to use the result since it is not going to wait for execution.– Just code
Nov 14 '18 at 4:56
add a comment |
1 Answer
1
active
oldest
votes
I would avoid this.. In short,
- There should be very little real-world uses-cases that would warrant it
- It could cause deadlocks
- It encourages you not use the
await
and propagate - It adds more branching in your method
- It adds more complexity when testing
- It generally smells bad. (IMO)
- Added by FCin, It will also generate AsyncStateMachine even if all calls are sync
Note : even writing async
and synchronous alternatives of the same method is fairly suspicious, its harder to maintain and maybe pointing to other design issues.
If you really want to wait for an async
method, let the caller do it, this at least give them the option to wrap it if needed or otherwise handle it appropriately
2
It will also generate AsyncStateMachine even if all calls are sync
– FCin
Nov 14 '18 at 5:24
@FCin added and attributed, thanks
– TheGeneral
Nov 14 '18 at 5:26
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53293350%2fadvantages-vs-disadvantages-bool-value-in-the-parameter-list-to-indicate-async%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I would avoid this.. In short,
- There should be very little real-world uses-cases that would warrant it
- It could cause deadlocks
- It encourages you not use the
await
and propagate - It adds more branching in your method
- It adds more complexity when testing
- It generally smells bad. (IMO)
- Added by FCin, It will also generate AsyncStateMachine even if all calls are sync
Note : even writing async
and synchronous alternatives of the same method is fairly suspicious, its harder to maintain and maybe pointing to other design issues.
If you really want to wait for an async
method, let the caller do it, this at least give them the option to wrap it if needed or otherwise handle it appropriately
2
It will also generate AsyncStateMachine even if all calls are sync
– FCin
Nov 14 '18 at 5:24
@FCin added and attributed, thanks
– TheGeneral
Nov 14 '18 at 5:26
add a comment |
I would avoid this.. In short,
- There should be very little real-world uses-cases that would warrant it
- It could cause deadlocks
- It encourages you not use the
await
and propagate - It adds more branching in your method
- It adds more complexity when testing
- It generally smells bad. (IMO)
- Added by FCin, It will also generate AsyncStateMachine even if all calls are sync
Note : even writing async
and synchronous alternatives of the same method is fairly suspicious, its harder to maintain and maybe pointing to other design issues.
If you really want to wait for an async
method, let the caller do it, this at least give them the option to wrap it if needed or otherwise handle it appropriately
2
It will also generate AsyncStateMachine even if all calls are sync
– FCin
Nov 14 '18 at 5:24
@FCin added and attributed, thanks
– TheGeneral
Nov 14 '18 at 5:26
add a comment |
I would avoid this.. In short,
- There should be very little real-world uses-cases that would warrant it
- It could cause deadlocks
- It encourages you not use the
await
and propagate - It adds more branching in your method
- It adds more complexity when testing
- It generally smells bad. (IMO)
- Added by FCin, It will also generate AsyncStateMachine even if all calls are sync
Note : even writing async
and synchronous alternatives of the same method is fairly suspicious, its harder to maintain and maybe pointing to other design issues.
If you really want to wait for an async
method, let the caller do it, this at least give them the option to wrap it if needed or otherwise handle it appropriately
I would avoid this.. In short,
- There should be very little real-world uses-cases that would warrant it
- It could cause deadlocks
- It encourages you not use the
await
and propagate - It adds more branching in your method
- It adds more complexity when testing
- It generally smells bad. (IMO)
- Added by FCin, It will also generate AsyncStateMachine even if all calls are sync
Note : even writing async
and synchronous alternatives of the same method is fairly suspicious, its harder to maintain and maybe pointing to other design issues.
If you really want to wait for an async
method, let the caller do it, this at least give them the option to wrap it if needed or otherwise handle it appropriately
edited Nov 14 '18 at 5:26
answered Nov 14 '18 at 5:10
TheGeneralTheGeneral
30k63465
30k63465
2
It will also generate AsyncStateMachine even if all calls are sync
– FCin
Nov 14 '18 at 5:24
@FCin added and attributed, thanks
– TheGeneral
Nov 14 '18 at 5:26
add a comment |
2
It will also generate AsyncStateMachine even if all calls are sync
– FCin
Nov 14 '18 at 5:24
@FCin added and attributed, thanks
– TheGeneral
Nov 14 '18 at 5:26
2
2
It will also generate AsyncStateMachine even if all calls are sync
– FCin
Nov 14 '18 at 5:24
It will also generate AsyncStateMachine even if all calls are sync
– FCin
Nov 14 '18 at 5:24
@FCin added and attributed, thanks
– TheGeneral
Nov 14 '18 at 5:26
@FCin added and attributed, thanks
– TheGeneral
Nov 14 '18 at 5:26
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53293350%2fadvantages-vs-disadvantages-bool-value-in-the-parameter-list-to-indicate-async%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Do you observe such things in the NET framework? Or do you see things like
LoadData()
andLoadDataAsync
?– None of the Above
Nov 14 '18 at 4:50
@Disaffected1070452 I just edited my question. Please refer to the examples added for your reference.
– Leon
Nov 14 '18 at 4:53
1
disadvantage in
async = false
you will not be able to use the result since it is not going to wait for execution.– Just code
Nov 14 '18 at 4:56