3

I'm refactoring some legacy code. I get error from jshint about cyclomatic complexity which I'm trying to figure out how to fix the warning. The code is in node.js so anything in JavaScript is very much welcome.

  if (rawObj.title) {
    formattedObj.name = rawObj.title;
  }
  if (rawObj.urls && rawObj.urls.web) {
    formattedObj.url = rawObj.urls.web.project;
  }
  if (rawObj.photo) {
    formattedObj.image = rawObj.photo.thumb;
  }
  if (rawObj.category) {
    formattedObj.category = rawObj.category.name;
  }

It's really just checking if the property exists and map to a new object.

toy
  • 11,711
  • 24
  • 93
  • 176
  • `I get error from jshint about cyclomatic complexity` - not with that code you dont – Jaromanda X Jan 11 '16 at 22:57
  • This piece of code does not have a large cyclomatic complexity - depending on how it is computed I expect it to be a value from 3 to at most 5. Possibly it is part of a larger piece of code, or you have a really low threshold for getting a warning. – Dirk Herrmann Jan 11 '16 at 22:57
  • sorry I missed out some of the code. – toy Jan 11 '16 at 22:59
  • But is there anyway I could reduce that chunk of code? – toy Jan 11 '16 at 22:59
  • 2
    Can you post the full function? This code looks fine to me, I wouldn't change anything about the structure. – Halcyon Jan 11 '16 at 23:13

1 Answers1

1

Kind of late to the party but you (or others looking for ways to reduce cyclomatic-complexity) could go with an approach like this. It's kind of like the strategy pattern. Depending if you can or can't use ES6, will determine which setRawObjProp you should use.

function setFormObjName () {
  formattedObj.name = rawObj.title;
  console.log(arguments.callee.name,formattedObj);
}

function setFormObjURL () {
  formattedObj.url = rawObj.urls.web.project;
  console.log(arguments.callee.name,formattedObj);
}

function setFormObjImage () {
  formattedObj.image = rawObj.photo.thumb;
  console.log(arguments.callee.name,formattedObj);
}

function setFormObjCat () {
  formattedObj.category = rawObj.category.name;
  console.log(arguments.callee.name,formattedObj);
}

function setRawObjProp(obj) {
  var objectMap = new Map();

  objectMap
    .set('string1', setFormObjName)
    .set('string2', setFormObjURL)
    .set('string3', setFormObjImage)
    .set('string4', setFormObjCat);

  if (objectMap.has(obj)) {
    return objectMap.get(obj)();
  }
  else {
    console.log('error', obj); 
  }
}

/*
function setRawObjProp2(obj) {
  var objectMap = {
      'string1': setFormObjName,
      'string2': setFormObjURL,
      'string3': setFormObjImage,
      'string4': setFormObjCat,
  };

  if (objectMap.hasOwnProperty(obj)) {
    return objectMap.get(obj)();
  }
  else {
    console.log('error', obj); 
  }
}
*/

var rawObj = {
  title: 'string1',
  urls: {
    app: {
      project: 'some thing'
    },
    web: {
      project: 'string2'
    }
  },
  photo: {
    large: 'large',
    thumb: 'string3'
  },
  category: {
    name: 'string4',
    type: 'some type',
    id: 12345
  }
},

formattedObj = {
  title: '',
  urls: {
    web: {
      project: ''
    }
  },
  photo: {
    thumb: ''
  },
  category: {
    name: ''
  }
};

setRawObjProp('string1');
/* setRawObjProp2('string1') */
colecmc
  • 3,133
  • 2
  • 20
  • 33