Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

200
Visualizações
Refactor this method to reduce its Cognitive Complexity from 16 to the 15 allowed. How to refactor and reduce the complexity?

How can I reduce the complexity of the bellow piece of code? I am getting this error in SonarQube:

Refactor this method to reduce its Cognitive Complexity from 16 to the 15 allowed.

(function () {
  window.dm = window.dm || { AjaxData: [] };
  window.dm.AjaxEvent = function (et, d, ssid, ad) {
    dm.AjaxData.push({
      et, d, ssid, ad,
    });
    window.DotMetricsObj && DotMetricsObj.onAjaxDataUpdate();
  };
  const d = document;
  const h = d.getElementsByTagName('head')[0];
  const s = d.createElement('script');
  let t = 'inews';
  s.type = 'text/javascript';
  s.async = true;

  if (window.PageContext.categories) {
    for (let category of window.PageContext.categories) {
      if (Utils.categoryMap[category.slug]) {
        t = Utils.categoryMap[category.slug];
        break;
      }
    }
  }

  if (window.PageContext.post && window.PageContext.post.breadcrumbs) {
    for (let category of window.PageContext.post.breadcrumbs.reverse()) {
      if (Utils.categoryMap[category.slug]) {
        t = Utils.categoryMap[category.slug];
        break;
      }
    }
  }

}());

export default () => { };
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can for example extract the complex if blocks to separate methods.

I mean these if blocks:

Extract to method 1 (eg handleCategories):

  if (window.PageContext.categories) {
    for (let category of window.PageContext.categories) {
      if (Utils.categoryMap[category.slug]) {
        t = Utils.categoryMap[category.slug];
        break;
      }
    }
  }

Extract to method 2 (eg handleBreadcrumbs):

  if (window.PageContext.post && window.PageContext.post.breadcrumbs) {
    for (let category of window.PageContext.post.breadcrumbs.reverse()) {
      if (Utils.categoryMap[category.slug]) {
        t = Utils.categoryMap[category.slug];
        break;
      }
    }
  }

This will move around 3 complexity levels (if + for + if) from the original method to each of the extracted methods.

about 4 years ago · Juan Pablo Isaza Relatório

0

SonarQube should be telling you where each complexity point comes from. You'll see the most complexity accrued on those inner if statements. So anything you can do to reduce their complexity count is going to help a lot.

Inverted condition & early return is a favorite tactic of mine. Here's how it can help in this situation:

(function () {
  window.dm = window.dm || { AjaxData: [] };
  window.dm.AjaxEvent = function (et, d, ssid, ad) {
    dm.AjaxData.push({
      et, d, ssid, ad,
    });
    window.DotMetricsObj && DotMetricsObj.onAjaxDataUpdate();
  };
  const d = document;
  const h = d.getElementsByTagName('head')[0];
  const s = d.createElement('script');
  let t = 'inews';
  s.type = 'text/javascript';
  s.async = true;

  if (!window.PageContext.categories) {
    for (let category of window.PageContext.categories) {
      if (Utils.categoryMap[category.slug]) {
        t = Utils.categoryMap[category.slug];
        break;
      }
    }
  }

  if (!window.PageContext.post || !window.PageContext.post.breadcrumbs) {
    return;
  }
  for (let category of window.PageContext.post.breadcrumbs.reverse()) {
    if (Utils.categoryMap[category.slug]) {
      t = Utils.categoryMap[category.slug];
      break;
    }
  }

}());
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda