Refactoring Sonarqube code

Asked

Viewed 136 times

3

What could I do to improve this code? Sonarqube indicates that I should refactor this code, but does not present any suggestions:

SonarQube indicando problemas de Cognitive Complexity

var dados = Set.Where(s => s.Id > 0);

    if (filtro.IdSolicitacao > 0)
    {
       dados = dados.Where(d => d.Id == filtro.IdSolicitacao);
    }
    else
    {
      if (filtro.IdFornecedor != 0)
        dados = dados.Where(d => d.IdPessoa == filtro.IdFornecedor);
      if (filtro.IdUsuarioSolicitacao != 0)
        dados = dados.Where(d => d.IdUsuarioSolicitacao == filtro.IdUsuarioSolicitacao);
      if (filtro.IdUsuarioAutorizacao != 0)
        dados = dados.Where(d => d.IdUsuarioAutorizacao == filtro.IdUsuarioAutorizacao);
      if (filtro.DataInicioSolicitacao != null)
        dados = dados.Where(d => d.DataSolicitacao >= filtro.DataInicioSolicitacao);
      if (filtro.DataFinalSolicitacao != null)
        dados = dados.Where(d => d.DataSolicitacao <= filtro.DataFinalSolicitacao);
      if (filtro.DataInicioAutorizacao != null)
        dados = dados.Where(d => d.DataAutorizacao >= filtro.DataInicioAutorizacao);
      if (filtro.DataFinalAutorizacao != null)
        dados = dados.Where(d => d.DataAutorizacao <= filtro.DataFinalAutorizacao);
      if (filtro.DataInicioVctoSolicitacao != null)
        dados = dados.Where(d => d.DataPagamentoAntecipacao >= filtro.DataInicioVctoSolicitacao);
      if (filtro.DataFinalVctoSolicitacao != null)
        dados = dados.Where(d => d.DataPagamentoAntecipacao <= filtro.DataFinalVctoSolicitacao);
      if (filtro.SituacaoSolicitacao != null)
      {
          var situacao = (SituacaoAntecipacaoFundoFinanceiro)filtro.SituacaoSolicitacao;
          dados = dados.Where(d => d.SituacaoSolicitacao == situacao);
      }
   }
  • Maybe putting it all on one LINQ command.

1 answer

3


This was answered in: Sonarlint, complexity of the "equals method()". Because the question is to decrease the number of ifs , but the cognitive complexity will continue, tragic if he considers it not. Then the way to silence it will be to separate in various method what can harm the code in performance and even legibility.

Note that if you change everything on a LINQ only really can give gains, even performance, but depends on context. And it can even give another result in some cases. To affirm this we would have to know that LINQ is this. Depending on him, you have to do it in a very different way than this. Without context an answer can cause more damage than a solution, and the person who does not understand the motivation or perceive it. For me the whole code needs to be refactored, but with this alone we can’t help.

But what is more important is that this does not reduce the complexity of fact, which makes question the quality of these software. It can be well used if you understand everything it proposes, so it serves only to indicate something you didn’t realize you could do. Because he has questionable rules and doesn’t explain them, he can cause more harm than good. He can start to make the code worse to comply with what he says.

That’s why I always say you need to understand the basics of not relying on third parties bossing you and not being able to question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.