我试图计算一些圈复杂度,因此试图绘制一个控制流图。首先,我试图使它成为一个相当简单的方法。
首先,我试着把它画成这样的尝试部分:

的方法:
[HttpPost]
public ActionResult GraphMethod([FromForm]string str)
{
try
{
int affectedRows = this._copyManager.CreateCopy(str);
if (affectedRows < 1) return BadRequest("Error!");
return Ok();
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}如何将其扩展到包括整个方法和try部分?
这是我第一次使用控制流图,所以如果我搞砸了,我也想知道。
发布于 2019-05-21 10:32:04
我会创建一个TryCreateCopy方法,并做一些与@saya的答案非常相似的事情
就像这样:
[HttpPost]
public ActionResult GraphMethod([FromForm]string str)
{
// These two if statements can be concatenated into one,
// but that would be a bit hard to read
if (this._copyManager.TryCreateCopy(str, out var affectedRows))
if (affectedRows > 1)
return Ok();
return BadRequest("Error!");
}
// _copyManager Method, there's probably a better way for you
public bool TryCreateCopy(string str, out int affectedRows)
{
try
{
affectedRows = CreateCopy(str);
}
// Please also don't do `catch (Exception)`,
// if you know which exception gets thrown always catch that
catch (Exception e)
{
affectedRows = -1;
return false;
}
return true;
}在没有抛出异常的情况下,创建副本时,TryCreateCopy方法返回true;如果抛出一个副本,则返回false *和带有受影响行数的out变量。
*可能有比我向您展示的更好的方法(例如验证方法?)因为尝试/捕获是相当资源密集的。
发布于 2019-05-21 09:36:56
就我而言,我命令您使用这段代码,更简单,更高效。
[HttpPost]
public ActionResult GraphMethod([FromForm]string str)
{
if (this._copyManager.CreateCopy(str) < 1)
return BadRequest("Error!");
return Ok();
}https://stackoverflow.com/questions/56234896
复制相似问题