【问题标题】:How can I refactor the code in order to remove the break statement in a C# application如何重构代码以删除 C# 应用程序中的 break 语句
【发布时间】:2019-06-29 00:03:53
【问题描述】:

我有一个 C# Windows 应用程序,我正在使用以下代码调用 API:

while (true)
{
    try
    {
        using (HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Get, "Some URL"))
        {
            requestMessage.Headers.Add("Accept", "application/json");                                
            response = await myHttpHelper.SendHttpRequest(requestMessage).ConfigureAwait(false);                             
        }
        break; // where the code smells is shown
    }
    catch (TaskCanceledException )
    {
        if (++attemptCount > 3)
        {
            throw;
        }
        Thread.Sleep(10000);
    }
    catch (Exception ex2)
    {
        throw;
    }
}

通常情况下,一旦出现网络问题,对 API 的获取请求就会被取消。所以我所做的是每当任务被取消时,我都会尝试三遍。如果它不起作用,那么我会向调用方法抛出异常。如果这 3 次尝试都成功,我将打破循环。

现在,当我对我的代码运行声纳分析时,它显示要删除 break 语句并重构代码。我该怎么做?

【问题讨论】:

  • 也许尝试在您的 while 循环中使用尝试计数?
  • 我建议考虑使用Polly。这比自己手动设置重试案例要容易得多。

标签: c# sonarqube break


【解决方案1】:

while (true) 是一个无限循环。 相反,我宁愿使用(布尔)变量来签入while()。 这样您就有机会将此变量设置为 false 并避免使用 break

【讨论】:

    【解决方案2】:

    您通常不应该使用while (true)(这是一个潜在的无限循环),即使您在某处有一个break 语句,有时也可能会发生此语句可能无法到达。

    我会推荐一个for 循环:

    for (int i = 0; i < 3; i++)
    {
        try
        {
            using (HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Get, "Some URL"))
            {
                requestMessage.Headers.Add("Accept", "application/json");                                
                response = await myHttpHelper.SendHttpRequest(requestMessage).ConfigureAwait(false);                             
            }
        }
        catch (TaskCanceledException)
        {
            Thread.Sleep(10000);
        }
        catch (Exception ex2)
        {
            throw;
        }
    }
    

    【讨论】:

    • 这不会发送消息 3 次吗?
    • @Magnus 是的,它会的,但我认为这将是一个关于整体改进代码的不同问题
    【解决方案3】:

    创建一个名为 success 的布尔变量并将其用作 while 循环条件:

    boolean success = false;
    while (!success) {
        try
        {
            using (HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Get, "Some URL"))
            {
                requestMessage.Headers.Add("Accept", "application/json");                                
                response = await myHttpHelper.SendHttpRequest(requestMessage).ConfigureAwait(false);                             
            }
            success = true; <--- set to true here
        }
        ...
    }
    

    【讨论】:

      【解决方案4】:

      如果您不选择 Polly,我建议您结合使用 @Sweeper 和 @Magnus 的回复:

      const int retryLimit = 3;
      boolean success = false;
      int retryCounter = 0;
      
      while (!success
              && retryCounter++ < retryLimit) {
          try
          {
              // http request as is
      
              success = true; 
          }
          // Exception handling
      }
      

      这样你控制重试次数并且请求成功时跳出循环。

      【讨论】:

        猜你喜欢
        • 2017-09-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-10
        • 1970-01-01
        • 2015-11-11
        • 1970-01-01
        相关资源
        最近更新 更多