Refactor SonarQube code

3

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

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);
      }
   }
    
asked by anonymous 27.08.2018 / 15:18

1 answer

3

This was answered here: SonarLint, complexity of the "equals ()" method . Because the issue is to decrease the number of if s, but the cognitive complexity will continue, tragic if he considers it not. So the way to silence it will be to separate in several methods which can damage the code in performance and even readability.

Note that if you change everything in a LINQ only really can give gains, even performance, but it depends on context. And it may even give you another result in some cases. To say this we would have to know that LINQ is this. I have to do it in a very different way from this. Without context a response can do more damage than solution, and the person who does not understand the motivation nor realize. For me the whole code needs to be refactored, but this alone we can not help.

But what is more important is that this does not actually reduce complexity, which raises questions about the quality of these software. It can be well used if you understand everything it proposes, so it is only meant to indicate something that you did not realize you could do. Because he has questionable rules and does not explain them, he can do more harm than good. You can start worsening the code to comply with what it says.

That's why I always say that you need to understand the fundamentals so you do not rely on third parties to send you or even question you.

    
27.08.2018 / 16:52