kisstkondoros / codemetrics

VSCode extension which shows the complexity information for TypeScript class members
Other
404 stars 20 forks source link

`return` make code complexity?? #47

Closed tangweikun closed 6 years ago

tangweikun commented 6 years ago

Every time add one return, the complexity will increase 1, it's a cool rule? That's my first time know about return make code complexity, can you help me understand about this!

kisstkondoros commented 6 years ago

You might have heard about the principle of single-entry single-exit. Or to make it more clear have a look at the page about control flow graph

So the point is, that adding a return statement can make the code harder to grasp.

consider the following as an example:

tagNameToType(tagname: string): string {
    switch (tagname) {
        case "div": return "element";
        case "paper-input":
        case "input": return "input";
        default:
            return undefined;
    }
}

vs.

tagNameToType(tagname: string): string {
    let type: string;
    switch (tagname) {
        case "div": type = "element";
        case "paper-input":
        case "input": type = "input";
        default:
            type = undefined;
    }
    return type;
}

For me at least it is easier to understand what the latter does. Since it might be a matter of personal taste however, it can be configured (also many other things) in the workspace / user settings:

"codemetrics.nodeconfiguration.ReturnStatement": 0
tangweikun commented 6 years ago

Thanks very much, this plugin is pretty awesome That return staff is about my personal style, so I will keep use return as before