[Code quality] code quality control - Complexity Detection (JavaScript)

 Reprinted from: https://juejin.im/post/59bb8b546fb9a00a4247532e

 

background

Code complexity is one of the important criteria for evaluating a project. Lower maintenance costs will reduce the complexity of the project, but also to avoid some uncontrollable problems. However, without a clear standard to measure the complexity of the structure of the code in the daily development, we can only relying on experience to assess the complexity of the structure of the code, for example, the extent of the code, the amount of branching structure and so on. The current code complexity in the end is what level? When we need to optimize code structure, reduce complexity? These problems we do not know.
Therefore, we need a clear set of criteria to measure the complexity of the code.

Metrics

LitmusIt is a code quality control system of our team-building, including the current code style checking, checking and repetition rate of the complexity of the examination. using litmus code Maintainability (maintainability) to measure the complexity of a code, and define the value Maintainability piece of code by the following three aspects:

  • Halstead Volume (code size)
  • Cyclomatic Complexity (cyclomatic complexity)
  • Lines of Code (lines of code)

The three parameters calculated Maintainability, i.e. code maintainability formula as follows:

Maintainability Index = MAX(0,(171 - 5.2 * ln(Halstead Volume) - 0.23 * (Cyclomatic Complexity) - 16.2 * ln(Lines of Code))*100 / 171)复制代码

The number of lines of code not go into details, let's introduce specific code size, complex meaning circle as well as their calculation principles

Halstead Volume (code size)

The capacity of the code is concerned that the number of code words, there are several basic concepts

parameter meaning
n1 Number of unique operators, the number of different operand (operator) of
n2 of unique operands, the number of distinct operands (operator) Number of
N1 Number of total occurrence of operators, is the number of all operand (operator) appears Total
N2 Number Number of total occurrence of operands, the operands for all (operator) appears Total
Vocabulary n1 + n2, the number of words
length N1 + N2, the length
Volume length * Log2 Vocabulary,容量

one example

function tFunc(opt) {
    let result = opt + 1;
    return result;
}
// n1:function,let,=,+,return // n2:tFunc,opt,result,1 // N1: function,let,=,+,return // N2:tFunc,opt,result,opt,1,result // Vocabulary = n1 + n2 = 9 // length = N1 + N2 = 11 // Volume = length * Log2 Vocabulary = 34.869复制代码

Cyclomatic Complexity (cyclomatic complexity)

concept

Cyclomatic complexity (Cyclomatic complexity, abbreviated CC) also known conditions complexity measure is one kind of code complexity. · J · by Thomas McCabe (Thomas J. McCabe, Sr.) made in 1976, used to represent the complexity of the program, the symbol VG or M. It can be used to measure the complexity of a module structure determination, expressed as number of independent current path number, the number of test cases can also be understood to cover all possible cases least used. Description cyclomatic complexity degree determination logic program code complexity, quality may be low and difficult to test and maintain. Possible errors and high complexity Circle program has a great relationship.

How to calculate

 


If an increase of the path from the end point to the starting point in the control flow graph, the entire flow pattern into a closed loop. Cyclomatic complexity is actually in this closed loop circuit, the number of linearly independent.

 

 


As shown, there are linearly independent circuits:

 

  • e1 → e2 → and
  • e1 e3 → → e

So complexity is 2
for simple graph, we can count, but for complex plans, this approach is not a wise choice.

The formula

V(G) = e – n + 2 * p复制代码
  • e: the number of edges in the control flow graph (a corresponding portion of the code sequence structure)
  • n: number of nodes in the representative decision control flow graph, including (branch statements in the code corresponding to) the start and end
    • ps: all end points is calculated only once, even if multiple return or throw
  • The number of independent components: p

Several common statement control flow graph

 

 

one example

code

function test(index, string) {
       let returnString;
       if (index == 1) { if (string.length < 2) { return '分支1'; } returnString = "returnString1"; } else if (index == 2) { if (string.length < 5) { return '分支2'; } returnString = "returnString2"; } else { return '分支3' } return returnString; }复制代码

flow-chart

flow-chartflow-chart
flow-graph
flow-graphflow-graph
计算

 

e(边):9
n(判定节点):6
p:1
V = e - n + 2 * p = 5复制代码

How to optimize

Mainly for cyclomatic complexity

The general direction: decrease of branching and looping determined using

(Some of the following examples may be cited not appropriate, only to illustrate such a process)

Refining function

// 优化前,圈复杂度4
function a (type) {
    if (type === 'name') { return `name:${type}`; } else if (type === 'age') { return `age:${type}`; } else if (type === 'sex') { return `sex:${type}`; } } // 优化后,圈复杂度1 function getName () { return `name:${type}`; } function getAge () { return `age:${type}`; } function getSex () { return `sex:${type}`; }复制代码

Table drive

// 优化前,圈复杂度4
function a (type) {
    if (type === 'name') { return 'Ann'; } else if (type === 'age') { return 11; } else if (type === 'sex') { return 'female'; } } // 优化后,圈复杂度1 function a (type) { let obj = { 'name': 'Ann', 'age': 11, 'sex': 'female' }; return obj[type]; }复制代码

Simplified conditional expressions

// 优化前,圈复杂度4
function a (num) {
    if (num === 0) {
        return 0;
    } else if (num === 1) { return 1; } else if (num === 2) { return 2; } else { return 3; } } // 优化后,圈复杂度2 function a (num) { if ([0,1,2].indexOf(num) > -1) { return num; } else { return 3; } }复制代码

Simplify function

// 优化前,圈复杂度4
function a () {
    let str = ''; for (let i = 0; i < 10; i++) { str += 'a' + i; } return str } function b () { let str = ''; for (let i = 0; i < 10; i++) { str += 'b' + i; } return str } function c () { let str = ''; for (let i = 0; i < 10; i++) { str += 'c' + i; } return str } // 优化后,圈复杂度2 function a (type) { let str = ''; for (let i = 0; i < 10; i++) { str += type + i; } return str }复制代码

Detection Tool

  1. Local Detection: ES6-Plato
    npm install --save es6-plato
    es6-plato -r -d report ./复制代码
  2. Quality litmus test center
    该系统由我们团队开发,目前仅限美团点评公司内部使用, the system as part of theme

 

Home Home

 

 

Home Projects Home Projects

 

 

Detail Page - Overview Detail Page - Overview

 

 

Details page - code complexity detection details Details page - code complexity detection details

 

 

Details page - code complexity detection details Details page - code complexity detection details

 

 

Details page - code complexity detection details Details page - code complexity detection details

Author: US Mission Comments ordering
link: https: //juejin.im/post/59bb8b546fb9a00a4247532e
Source: Nuggets
copyright reserved by the authors. Commercial reprint please contact the author authorized, non-commercial reprint please indicate the source.

Guess you like

Origin www.cnblogs.com/0616--ataozhijia/p/11607977.html
Recommended