Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

205
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda