Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maxcomplexity - better complexity calculation #2312

Open
arthrp opened this issue Apr 14, 2015 · 3 comments
Open

Maxcomplexity - better complexity calculation #2312

arthrp opened this issue Apr 14, 2015 · 3 comments
Labels

Comments

@arthrp
Copy link

arthrp commented Apr 14, 2015

When cyclomatic complexity is high, it's believed that code was written in "spagetti code" style. Indeed, when there are a lot "linearly independent paths" through function/method, it probably was a good indicator of a complex code - at least in 1976 when the metric was developed.

However, I think that nowadays, with modern code syntax, this is not an ultimate indicator. Consider the following example:

function doStuff(obj){
  var ret = {};

 ret.q = obj['q'] || 'no q';
 ret.w = obj['w'] || 'no w';
 ret.q = obj['q'] || 'no e';
 ret.q = obj['q'] || 'no r';
 ret.q = obj['q'] || 'no t';
 ret.y = obj['q'] || 'no y';
 ret.u = obj['u'] || 'no u';
 ret.i = obj['i'] || 'no i';
 ret.o = obj['o'] || 'no o';
 ret.p = obj['p'] || 'no p';

 return ret;
}

JSHint says that cyclomatic complexity number for this function is 11. Why? Because I use the handy syntax for fallback values all over the place. Is this a spagetti code? Definitely not. Is it hard to read? Don't think so. Most important, those statements are independent, if you change the order, nothing will change.

I understand - if we rewrite the code in old way like

 if(obj['q']){
    ret.q = obj['q'];
}
else{
    ret.q = 'no q';
}

this function will start to look ugly. But I can't think of a single reason why anyone would want to do this.

Would you consider reducing the complexity introduced by certain constructs (such as aforementioned one)? Or at least making another metric which would calculate complexity closer to real life?

@rwaldron
Copy link
Member

Why would you write out "fallback" values in bulk like that? This has a cyclomatic complexity of 1:

function doStuff(obj) {
  var defaults = {
    q: 'no q', 
    w: 'no w', 
    e: 'no e', 
    r: 'no r', 
    t: 'no t', 
    y: 'no y', 
    u: 'no u', 
    i: 'no i', 
    o: 'no o', 
    p: 'no p',   
  };
  return Object.assign({}, defaults, obj);
}

Or at least making another metric which would calculate complexity closer to real life?

If it were up to me, I would take it out entirely.

@arthrp
Copy link
Author

arthrp commented Apr 15, 2015

@rwaldron

Why would you write out "fallback" values in bulk like that?

Because it reads just like a normal assignment? It's a widespread construct in modern languages, e.g. in C#:

int speed = obj.speed ?? 10;

Sorry, but I find it much less verbose and error-prone than Object.assign({}, defaults, obj); where making a typo in property name is easy as hell.

If you don't want to break the existing implementation, creating a new metric shouldn't be too hard? I think that if both is true:

  • Single ternary operator or null coalescing operator (don't know other name for that "fallback" thing) is used
  • Assignments are independent

it's safe to assume that code complexity is not increased.

@lukeapage
Copy link
Member

related #840

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants