How do I simplify this method? Is handling an exception in this method a good idea?
[WebMethod]
public string GetMDSToken(string xuid)
{
var tokenTask = _mapTokenService.GetMappedSSOTokenAsync(xuid);
var tokenTimestampTask = _SSOFrontendService.GetSSOTokenTimestampAsync();
Task.WhenAll(tokenTask, tokenTimestampTask);
if (tokenTimestampTask.Result.Status != ResultStatus.OK)
return _serializer.Serizalize(tokenTimestampTask.Result);
if (tokenTask.Result == null)
{
var response = _handledResponseFactory.CreateErrorResponce(ResultStatus.Business, NotLogginErrorMessage);
return _serializer.Serizalize(response);
}
try
{
var mappedToken = tokenTask.Result;
if (mappedToken.SSOTokenTimestamp != tokenTimestampTask.Result.Content)
UpdateSSOToken(mappedToken);
var MDSTokenTask = _MDSFrontendService.GetToken(mappedToken.SSOToken);
Task.WhenAll(MDSTokenTask);
return _serializer.Serizalize(MDSTokenTask.Result);
}
catch (Exception ex)
{
var response = _handledResponseFactory.CreateExceptionResponce(ex);
return _serializer.Serizalize(response);
}
}
1 Answer 1
(削除) I can't speak for the language you a using specifically, but I have always been lead to believe that declaring variables within if
and try
statements is bad form. Can't say I've ever seen one in a catch
statement, but I believe the same principle might apply. (削除ここまで)
You seem to be wrapping a lot in that try
block. Do you expect that
var mappedToken = tokenTask.Result;
could actually throw an exception? If not, I would personally recommend removing it from that code block.
To be clear, how much code to put in a try
block would be a matter of opinion, but are you obeying the single responsibility principle? I would posit that the code would be simplified if the reader knew what is expected to throw an exception at a glance. Your try
block is responsible for 6 different tasks by my count. Should the reader assume that all of those tasks are capable of faulting, and that the resulting exception should be the same regardless of which part failed and which part was successful? If UpdateSSOToken
were to execute but Task.WhenAll
failed are we in the same boat we would be in if UpdateSSOToken
had failed? Something to consider.
As for is it a good idea to have the exception at all-
The method you choose depends on how often you expect the event to occur.
To help you determine that you should consider the following about the WhenAll
method of Tasks
-
This answer cites some useful information on exception with tasks, including
which goes into detail about the await pattern. I would suggest at least reading the primer if none of the other links, I believe it directly addresses your question.
-
2\$\begingroup\$ In C#, variables should be declared as close as possible to their usage, and ideally be as short-lived as possible; declaring variables within an
if
or atry
block is ok. However you are correct,tokenTask.Result
is already accessed outside of thetry
block, hencemappedToken
should be declared and assigned outside thetry
block. \$\endgroup\$Mathieu Guindon– Mathieu Guindon2014年06月03日 22:28:25 +00:00Commented Jun 3, 2014 at 22:28 -
\$\begingroup\$ +1 @Mat'sMug Thanks for that tidbit, I did not realize that this only applied to JavaScript. Now I have to re-optimize my .NET classes to reflect this. Better late than never I guess... \$\endgroup\$blaze4218– blaze42182014年06月04日 16:02:23 +00:00Commented Jun 4, 2014 at 16:02