0

I'm building a simple ETL tool using Node.js. So, I get one json object and manipulate to another object. However, after running through Lint, I get

18:1 warning Function 'format' has a complexity of 5 complexity

This is the example of the code. I wish there's some JavaScript magic I could use.

  if (rawObj.attr1 && rawObj.attr2) {
    formattedObj.attr2 = rawObj.attr1;
  }
  if (rawObj.attr3) {
    formattedObj.otherAttr = rawObj.attr3;
  }
  if (rawObj.attr4) {
    formattedObj.otherAttr4 = rawObj.attr4;
  }
  formattedObj.rank = index + 1;

  if (rawObj.attr5) {
    formattedObj.otherAttr5 = rawObj.attr5;
  }

basically, it's just checking if the property is undefined or not. Then sets the property.

toy
  • 11,711
  • 24
  • 93
  • 176
  • 1
    Abstract out the copy? Use a general purpose transformation library? – Dave Newton Dec 03 '15 at 19:31
  • 1
    Not sure how your complexity is measured, but you can inline your if statements using the ternary operator so that variable assignment always takes place, like so: `formattedObj.attr2 = rawObj.attr1 && rawObj.attr2 ? rawObj.attr1 : undefined;`. There are other ways to decrease your complexity, but a general solution would limit you somewhat. –  Dec 03 '15 at 19:38
  • 1
    Would something like this work: http://stackoverflow.com/questions/2802055/what-does-the-construct-x-x-y-mean – Matthew Bakaitis Dec 03 '15 at 19:38
  • @jedd.ahyoung That doesn't change complexity, it just rewrites it. The same conditions exist. – Dave Newton Dec 03 '15 at 19:44
  • @DaveNewton Then, depending on your transformations, there's nothing you can do. If it's a one-to-one mapping (like rawObj.attr5) you can simply remove the if statement, as you'll be assigning `undefined` to an already undefined variable - that would solve your complexity issue. For cases like the first, though, Ithere is not a way, as you'll always have that conditional somewhere in your code. –  Dec 03 '15 at 20:20
  • Also - note that the way you have things now - you'll be tripped up by falsy values. You may want to specify `== null` or something like that depending on what you're looking for. (An empty string, or `0`, etc, will cause your conditionals to fail.) –  Dec 03 '15 at 20:21
  • @jedd.ahyoung Correct. Swapping conditional constructs doesn't affect cyclomatic complexity, only reducing complexity does. – Dave Newton Dec 03 '15 at 20:42

1 Answers1

2

You're repeating yourself. Use a loop instead:

var props = [
    {from: "attr3", to:"otherAttr"},
    {from: "attr4", to:"otherAttr4"},
    {from: "attr5", to:"otherAttr5"}
];
if (rawObj.attr2)
    props.push({from: "attr1", to:"attr2"});

props.forEach(function(p) {
    if (rawObj[p.from])
        formattedObj[p.to] = rawObj[p.from];
});
formattedObj.rank = index + 1;
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Does this actually reduce the complexity? For static analysis, perhaps, but I don't think this actually works....unless I don't fully understand the definition. –  Dec 10 '15 at 18:23
  • @jedd.ahyoung What do you think does not work about it? – Bergi Dec 10 '15 at 18:27