V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
xcatliu
V2EX  ›  程序员

运行一个脚本,看看你的项目的代码质量吧

  •  3
     
  •   xcatliu ·
    xcatliu · 2017-09-20 18:18:45 +08:00 · 15137 次点击
    这是一个创建于 2381 天前的主题,其中的信息可能已经有所发展或是发生改变。

    代码质量有很多指标:

    1. 源代码行数
    2. 代码重复率
    3. 圈复杂度
    4. 报错量( Bug 数)占比
    5. 测试覆盖率
    6. 开发约束(代码块行数等)

    我做了一个脚本可以测出上面的 1, 2, 3

    大家都来试一试吧!

    Installation

    npm install -g cqc
    

    Usage

    cqc [options] <pattern ...>
    

    Examples:

    cqc src/**/*.js
    

    Output:

    Number of files:            8
    Souce lines of code:        357
    Duplicate rate:             5.62%
    Max complexity:             15
    Complexity > 5  (count):    3
    Complexity > 10 (count):    1
    Complexity > 20 (count):    0
    

    Multiple patterns

    cqc src/**/*.js src/**/*.jsx
    

    --ignore-pattern

    cqc src/**/*.js --ignore-pattern src/vendor/**/*.js
    
    cqc src/**/*.js --ignore-pattern src/vendor/**/*.js,src/third-party/**/*.js
    

    --ignore-file

    cqc src/**/*.js --ignore-file .gitignore
    
    cqc src/**/*.js --ignore-file .gitignore,.eslintignore
    

    --format

    cqc src/**/*.js --format json
    

    Output:

    {
        "numberOfFiles": 8,
        "sloc": {
            "source": 357
        },
        "jscpd": {
            "percentage": "5.62"
        },
        "complexity": {
            "max": 15,
            "gt5Count": 3,
            "gt10Count": 1,
            "gt20Count": 0
        }
    }
    
    第 1 条附言  ·  2017-09-20 19:10:48 +08:00

    文档写错了,--ignore-file 应该是 --ignore-path

    另外还有一个参数是 --verbose 可以打印出更多信息

    第 2 条附言  ·  2017-09-21 14:16:12 +08:00

    v0.2.0

    更新输出格式为:

    Number of files:        9
    Source lines of code:   463
    Duplicate rate:         15.71%
    High complexity rate:   11.11%
    Max complexity:         19
    

    增加 --complexity-threshold 参数,默认为 10

    86 条回复    2017-09-22 23:43:34 +08:00
    xcatliu
        1
    xcatliu  
    OP
       2017-09-20 18:25:45 +08:00
    先来贴一个我们团队的项目的:

    Number of files: 997
    Souce lines of code: 94524
    Duplicate rate: 12.17%
    Max complexity: 89
    Complexity > 5 (count): 292
    Complexity > 10 (count): 76
    Complexity > 20 (count): 17

    重复代码率有点高,一般在 5% 以内比较正常。
    最大圈复杂度有点太大了。。。
    iluhcm
        2
    iluhcm  
       2017-09-20 18:50:16 +08:00   ❤️ 1
    试了一下,Mac 运行报错。

    env: node\r: No such file or directory

    Node:v8.1.3
    xcatliu
        3
    xcatliu  
    OP
       2017-09-20 19:02:41 +08:00
    @iluhcm 感谢反馈,已修复,是换行符的问题。
    可以安装新版本试试啦
    npm i [email protected] -g
    siteshen
        4
    siteshen  
       2017-09-20 19:10:35 +08:00   ❤️ 4
    看标题还以为支持所有语言,结果打开 GitHub 才发现只支持前端的内容。

    发在“程序员”节点,希望标题里有“前端”、“ Javascript ” 之类的字样。
    xcatliu
        5
    xcatliu  
    OP
       2017-09-20 19:13:20 +08:00
    @siteshen 因为 complexity 是用 eslint 实现的,所以暂不支持其他语言的 complexity。重复度检查是给予 jscpd 的,所以支持很多语言。
    spring5413
        6
    spring5413  
       2017-09-20 19:31:47 +08:00   ❤️ 1
    请问大神,这个检查重复原理是什么
    vardarling
        7
    vardarling  
       2017-09-20 19:46:33 +08:00   ❤️ 1
    "Souce lines of code" bug +1 😆
    xcatliu
        8
    xcatliu  
    OP
       2017-09-20 19:51:04 +08:00 via iPhone
    @vardarling lines of source code 😂
    xcatliu
        9
    xcatliu  
    OP
       2017-09-20 19:55:41 +08:00 via iPhone
    @vardarling 第一次还没看出来,原来是少了个 r 😂
    xcatliu
        10
    xcatliu  
    OP
       2017-09-20 20:02:36 +08:00
    @vardarling 已修复,谢谢反馈 😀
    xcatliu
        11
    xcatliu  
    OP
       2017-09-20 20:04:12 +08:00
    @spring5413 是基于 jscpd 的,原理应该就是静态的分析两个文件有没有连续的重复行,超过五行即视为代码有重复。
    https://github.com/kucherenko/jscpd
    vardarling
        12
    vardarling  
       2017-09-20 20:06:15 +08:00
    @xcatliu 第一次发现我眼睛这么好😂
    zhlssg
        13
    zhlssg  
       2017-09-20 20:53:02 +08:00 via iPhone
    这个有点厉害
    mhycy
        14
    mhycy  
       2017-09-20 22:05:49 +08:00   ❤️ 12
    “您的代码质量分已超过 90%程序员,点击按钮一键优化”
    xcatliu
        15
    xcatliu  
    OP
       2017-09-20 22:22:20 +08:00 via iPhone
    @mhycy 这个可以😂
    AlloVince
        16
    AlloVince  
       2017-09-20 22:23:50 +08:00   ❤️ 1
    Number of files: 342
    Source lines of code: 38967
    Duplicate rate: 3.29%
    Max complexity: 27
    Complexity > 5 (count): 73
    Complexity > 10 (count): 18
    Complexity > 20 (count): 2
    xcatliu
        17
    xcatliu  
    OP
       2017-09-20 22:53:12 +08:00
    @AlloVince 很健康的数据啊
    AlloVince
        18
    AlloVince  
       2017-09-20 22:56:40 +08:00   ❤️ 1
    @xcatliu 不知道源代码里一些 ES 的语法是怎么处理的,扫描的源码里使用了 decorators,object rest spread 等一些需要 babel 的语法
    binux
        19
    binux  
       2017-09-20 23:12:50 +08:00   ❤️ 1
    Number of files: 95
    Source lines of code: 6233
    Duplicate rate: 0.19%
    Max complexity: 45
    Complexity > 5 (count): 18
    Complexity > 10 (count): 6
    Complexity > 20 (count): 3
    momocraft
        20
    momocraft  
       2017-09-20 23:17:00 +08:00   ❤️ 1
    cqc... 这名字怎么这么谐
    Sapp
        21
    Sapp  
       2017-09-20 23:42:36 +08:00   ❤️ 1
    Number of files: 1
    Souce lines of code: 57915
    Duplicate rate: 6.95%
    Max complexity: 72
    Complexity > 5 (count): 1
    Complexity > 10 (count): 1
    Complexity > 20 (count): 1

    测了一下打包后的。
    orange666
        22
    orange666  
       2017-09-21 08:45:04 +08:00   ❤️ 1
    新手一枚,看到就试试,怎么用的这玩意。
    imlinhanchao
        23
    imlinhanchao  
       2017-09-21 09:02:46 +08:00   ❤️ 1
    记得 Coding 自带这个功能,而且不仅支持前端代码。 @Bazingawang
    iugo
        24
    iugo  
       2017-09-21 09:18:04 +08:00
    不知道 pattern 该怎么写.

    `cqc src/**/*.js src/**/*.jsx` 却只找到一个 jsx 文件...
    polun
        25
    polun  
       2017-09-21 09:18:16 +08:00   ❤️ 1
    报错,Linux
    Node: 6.11.0

    fs.js:732
    var r = binding.read(fd, buffer, offset, length, position);
    ^

    Error: EISDIR: illegal operation on a directory, read
    at Error (native)
    at Object.fs.readSync (fs.js:732:19)
    at tryReadSync (fs.js:487:20)
    at Object.fs.readFileSync (fs.js:535:19)
    at slocResult.fileList.reduce (/home/polunzh/.nvm/versions/node/v6.11.0/lib/node_modules/cqc/src/SlocChecker/index.js:14:36)
    at Array.reduce (native)
    at SlocChecker.check (/home/polunzh/.nvm/versions/node/v6.11.0/lib/node_modules/cqc/src/SlocChecker/index.js:13:42)
    at CodeQualityChecker.check (/home/polunzh/.nvm/versions/node/v6.11.0/lib/node_modules/cqc/src/CodeQualityChecker/index.js:17:45)
    at Object.<anonymous> (/home/polunzh/.nvm/versions/node/v6.11.0/lib/node_modules/cqc/bin/cqc.js:41:38)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:389:7)
    iugo
        26
    iugo  
       2017-09-21 09:24:01 +08:00
    ```
    cqc src/**/*.jsx src/**/**/*.jsx src/**/**/**/*.jsx src/**/**/**/**/*.jsx src/**/*.js src/**/**/*.js src/**/**/**/*.js src/**/**/**/**/*.js

    Number of files: 557
    Source lines of code: 19665
    Duplicate rate: 9.61%
    Max complexity: 12
    Complexity > 5 (count): 10
    Complexity > 10 (count): 1
    Complexity > 20 (count): 0
    ```
    jjplay
        27
    jjplay  
       2017-09-21 09:30:03 +08:00   ❤️ 1
    cqc bin/cqc.js 会不会更棒!
    xcatliu
        28
    xcatliu  
    OP
       2017-09-21 09:32:23 +08:00 via iPhone
    @iugo 是不是在 windows 环境下?
    iugo
        29
    iugo  
       2017-09-21 09:38:05 +08:00
    @xcatliu macOS 10.12.6, VS Code 终端
    iugo
        30
    iugo  
       2017-09-21 09:39:00 +08:00
    @xcatliu Node v8.4.0 npm v5.4.2
    ma125125t
        31
    ma125125t  
       2017-09-21 09:41:01 +08:00   ❤️ 1
    有个问题我不明白,我的 mac 安装任何软件都需要 sudo 权限,否则就会提示 permission denied,无论用 npm,还是其他安装工具,这是为什么呢?
    xcatliu
        32
    xcatliu  
    OP
       2017-09-21 09:44:44 +08:00
    @iugo 试一下加上双引号呢? cqc "src/**/*.js" "src/**/*.jsx"
    xcatliu
        33
    xcatliu  
    OP
       2017-09-21 09:48:23 +08:00
    @AlloVince
    > 不知道源代码里一些 ES 的语法是怎么处理的,扫描的源码里使用了 decorators,object rest spread 等一些需要 babel 的语法

    代码行数和重复率检查是(基本上)语言无关的,所以可以识别。Complexity 是使用的 eslint 识别的,默认加上了 babel-eslint parser,所以应该可以识别 decorators,object rest spread 语法
    详见 https://github.com/xcatliu/cqc/blob/master/src/ComplexityChecker/eslintConfig.js
    xcatliu
        34
    xcatliu  
    OP
       2017-09-21 09:49:38 +08:00
    @binux 重复率 0.19% 是怎么做到的,厉害。
    xcatliu
        35
    xcatliu  
    OP
       2017-09-21 09:49:54 +08:00
    @momocraft cqc 就是 Code Quality Checker
    xcatliu
        36
    xcatliu  
    OP
       2017-09-21 09:50:19 +08:00
    @Sapp 测打包后的没什么意义吧
    page470075640
        37
    page470075640  
       2017-09-21 09:50:36 +08:00   ❤️ 1
    https://github.com/pagewang0/form-validation

    Number of files: 1
    Source lines of code: 207
    Duplicate rate: 0.00%
    Max complexity: 22
    Complexity > 5 (count): 1
    Complexity > 10 (count): 1
    Complexity > 20 (count): 1
    xcatliu
        38
    xcatliu  
    OP
       2017-09-21 09:51:28 +08:00
    @orange666 需要 node 环境。运行 npm i cqc -g 可以安装 cqc 命令,然后运行 cqc src/**/*.js 可以测所有 js 文件
    xcatliu
        39
    xcatliu  
    OP
       2017-09-21 09:52:10 +08:00
    @imlinhanchao 要支持其他语言的代码,应该需要一个一个去适配
    iugo
        40
    iugo  
       2017-09-21 09:53:04 +08:00   ❤️ 1
    @xcatliu Ok.
    VtoEXL
        41
    VtoEXL  
       2017-09-21 09:59:36 +08:00   ❤️ 1
    不支持.vue 文件
    xcatliu
        42
    xcatliu  
    OP
       2017-09-21 10:00:43 +08:00
    @polun
    > Error: EISDIR: illegal operation on a directory, read
    看 log 应该是把 dir 当成 file 去 read 了。
    已修复,可以试一下新版本还有没有问题 npm i -g [email protected]
    xcatliu
        43
    xcatliu  
    OP
       2017-09-21 10:02:04 +08:00
    @iugo 加双引号就可以了?估计是 glob 这块有点问题,我看看好不好修复
    xcatliu
        44
    xcatliu  
    OP
       2017-09-21 10:08:52 +08:00
    @jjplay cqc 自身的检测结果挺好的哇

    Number of files: 8
    Source lines of code: 360
    Duplicate rate: 0.00%
    Max complexity: 4
    Complexity > 5 (count): 0
    Complexity > 10 (count): 0
    Complexity > 20 (count): 0
    xcatliu
        45
    xcatliu  
    OP
       2017-09-21 10:09:51 +08:00
    @ma125125t 可以用 nvm 来安装 node 和 npm,应该就不需要 sudo 了吧
    xcatliu
        46
    xcatliu  
    OP
       2017-09-21 10:13:00 +08:00
    @page470075640 不错,不过 max complexity 有点高了,根据 [这个] 的说法:

    In general, for method level complexity:

    < 10 Easy to maintain
    11-20 Harder to maintain
    21+ Candidates for refactoring/redesign

    [这个]: https://softwareengineering.stackexchange.com/questions/101830/what-does-the-cyclomatic-complexity-of-my-code-mean
    xcatliu
        47
    xcatliu  
    OP
       2017-09-21 10:15:26 +08:00
    @VtoEXL sloc 暂时还不支持 vue 格式 https://github.com/flosse/sloc/issues/88
    Sapp
        48
    Sapp  
       2017-09-21 10:46:13 +08:00   ❤️ 1
    @xcatliu 因为不支持 vue。只能测打包的。
    fds
        49
    fds  
       2017-09-21 10:47:57 +08:00   ❤️ 1
    游戏服务器 ts 生成的 js 分析结果:

    Number of files: 188
    Source lines of code: 55663
    Duplicate rate: 22.44%
    Max complexity: 145
    Complexity > 5 (count): 97
    Complexity > 10 (count): 69
    Complexity > 20 (count): 26

    辣眼睛……正在翻阅 verbose 结果中……
    wyk52012
        50
    wyk52012  
       2017-09-21 10:53:15 +08:00   ❤️ 1
    公司有 sonar。。。。
    iugo
        51
    iugo  
       2017-09-21 11:27:48 +08:00
    @fds 游戏服务也可以用 Node 做? 传说中都是用 C++...
    xcatliu
        52
    xcatliu  
    OP
       2017-09-21 11:41:10 +08:00
    @wyk52012 sonar 确实是一个好工具!我这只是一个比较轻量的小工具
    xcatliu
        53
    xcatliu  
    OP
       2017-09-21 11:43:19 +08:00
    @fds Max complexity 145 也是厉害!
    dreamwar
        54
    dreamwar  
       2017-09-21 11:43:56 +08:00
    哈哈哈,很溜,有点像 360 开机时候提示你打败了全国多少人一样
    figofuture
        55
    figofuture  
       2017-09-21 11:47:10 +08:00
    mark 了
    SakuraKuma
        56
    SakuraKuma  
       2017-09-21 12:20:12 +08:00
    Number of files: 60
    Source lines of code: 16892
    Duplicate rate: 4.74%
    Max complexity: 39
    Complexity > 5 (count): 25
    Complexity > 10 (count): 10
    Complexity > 20 (count): 4

    emmmm
    page470075640
        57
    page470075640  
       2017-09-21 12:58:35 +08:00   ❤️ 1
    @xcatliu 我的那个两百多行的代码 我不晓得如何进一步改进了 还望指正
    jedihy
        58
    jedihy  
       2017-09-21 13:09:40 +08:00   ❤️ 1
    静态代码分析已经有非常成熟的产品了,上学期有门课的老师就是这个领域的大牛,可以试试这个
    https://deepscan.io
    NCE
        59
    NCE  
       2017-09-21 13:21:03 +08:00   ❤️ 1
    要解析一个项目需要多长时间?
    NCE
        60
    NCE  
       2017-09-21 13:21:32 +08:00
    Number of files: 1644
    Source lines of code: 129596
    Duplicate rate: 1.17%
    Max complexity: 0
    Complexity > 5 (count): 0
    Complexity > 10 (count): 0
    Complexity > 20 (count): 0
    orange666
        61
    orange666  
       2017-09-21 13:48:32 +08:00
    @xcatliu 酱紫啊 谢谢啊
    rashawn
        62
    rashawn  
       2017-09-21 14:05:31 +08:00
    Number of files: 286
    Source lines of code: 72966
    Duplicate rate: 2.91%
    Max complexity: 158
    Complexity > 5 (count): 146
    Complexity > 10 (count): 85
    Complexity > 20 (count): 31
    hanzichi
        63
    hanzichi  
       2017-09-21 15:48:06 +08:00   ❤️ 1
    cat 胸去 alloyteam 啦?
    xcatliu
        64
    xcatliu  
    OP
       2017-09-21 18:18:34 +08:00 via iPhone
    xcatliu
        65
    xcatliu  
    OP
       2017-09-21 18:19:37 +08:00 via iPhone
    @NCE 大概是每秒一万行代码吧
    page470075640
        66
    page470075640  
       2017-09-21 22:51:04 +08:00   ❤️ 1
    Number of files: 1
    Source lines of code: 192
    Duplicate rate: 0.00%
    Max complexity: 19
    Complexity > 5 (count): 1
    Complexity > 10 (count): 1
    Complexity > 20 (count): 0

    谢咯 感觉这个 Complexity 是时间复杂度对吧
    xcatliu
        67
    xcatliu  
    OP
       2017-09-22 00:17:23 +08:00
    @page470075640 这里的复杂度不是时间复杂度,是圈复杂度,也叫循环复杂度( Cyclomatic complexity ),就是程序的流程图里面的圈的数量。它决定了程序的可读性和可维护性。

    少于 10 易于维护
    多于 10 少于 20 难维护
    超过 20 就不可维护了
    xcatliu
        68
    xcatliu  
    OP
       2017-09-22 00:18:23 +08:00
    @jedihy 感谢,以前只知道 sonar,已收藏。
    jisi724
        69
    jisi724  
       2017-09-22 02:34:52 +08:00   ❤️ 1
    Number of files: 58
    Source lines of code: 1471
    Duplicate rate: 1.04%
    High complexity rate: 1.72%
    Max complexity: 14
    xcatliu
        70
    xcatliu  
    OP
       2017-09-22 07:26:02 +08:00 via iPhone
    @jisi724 赞,很健康
    k9982874
        71
    k9982874  
       2017-09-22 08:55:53 +08:00
    ```
    Number of files: 13
    Source lines of code: 1083
    Duplicate rate: 5.24%
    High complexity rate: 7.69%
    Max complexity: 19
    ```
    zoffy
        72
    zoffy  
       2017-09-22 09:18:54 +08:00
    比较好奇重复率的算法怎么写的
    fds
        73
    fds  
       2017-09-22 10:30:36 +08:00
    @iugo 游戏服务器还有 python 什么的呢。很多游戏计算量不大,就是读读数据库,用什么语言都差不多~
    fds
        74
    fds  
       2017-09-22 10:32:47 +08:00
    @NCE 你这分析结果估计是遇上了不支持的语言,不然复杂度不能这么低……
    qichunren
        75
    qichunren  
       2017-09-22 10:37:52 +08:00
    你的这个工具要是再支持一些其它的如 c,c++,ruby,python 之类的就好了。
    ThomasChan
        76
    ThomasChan  
       2017-09-22 11:08:38 +08:00
    Number of files: 280
    Source lines of code: 57742
    Duplicate rate: 7.52%
    High complexity rate: 26.43%
    Max complexity: 65


    complexity 超过 20 难以维护?。。。。😂😂😂
    pengfei
        77
    pengfei  
       2017-09-22 11:17:52 +08:00
    /i/h3P3KZWh.png
    代码有点少
    pengfei
        78
    pengfei  
       2017-09-22 11:18:02 +08:00
    sobigfish
        79
    sobigfish  
       2017-09-22 14:00:22 +08:00   ❤️ 1
    CQC 第一反应是 close quarters combat -。-

    其实我是来提醒作者 https://choosealicense.com/
    xcatliu
        80
    xcatliu  
    OP
       2017-09-22 14:38:04 +08:00
    @sobigfish 感谢提醒,这个项目依赖了 sloc, jscpd, eslint 等,他们都是 MIT 协议。
    我从开源社区的收获远比我的贡献多,所以我的所有项目几乎都是 MIT 协议,希望能够给开源社区贡献一点力量!
    jianghu521
        81
    jianghu521  
       2017-09-22 19:32:19 +08:00
    zsh: command not found: cpc @xcatliu
    xcatliu
        82
    xcatliu  
    OP
       2017-09-22 19:33:53 +08:00
    @jianghu521 cqc 不是 cpc 哈
    Hilong
        83
    Hilong  
       2017-09-22 19:44:39 +08:00 via Android
    不支持 vue 文件。。。
    jianghu521
        84
    jianghu521  
       2017-09-22 19:45:41 +08:00
    @xcatliu 嗯 谢谢 写错了
    jianghu521
        85
    jianghu521  
       2017-09-22 19:58:22 +08:00
    Number of files: 344
    Source lines of code: 26728
    Duplicate rate: 0.00%
    Max complexity: 0
    Complexity > 5 (count): 0
    Complexity > 10 (count): 0
    Complexity > 20 (count): 0

    @xcatliu 是不是有问题啊
    Maic
        86
    Maic  
       2017-09-22 23:43:34 +08:00
    ➜ git:(master) ✗ node_modules/.bin/cqc back/*

    Number of files: 8
    Source lines of code: 773
    Duplicate rate: 1.23%
    High complexity rate: 0.00%
    Max complexity: 8

    ➜ git:(master) ✗ node_modules/.bin/cqc back/*/*

    Number of files: 7
    Source lines of code: 419
    Duplicate rate: 0.00%
    High complexity rate: 0.00%
    Max complexity: 5

    ➜ git:(master) ✗ node_modules/.bin/cqc back/

    Number of files: 0
    Source lines of code: 0
    Duplicate rate: 0.00%
    High complexity rate: NaN%
    Max complexity: 0

    ➜ git:(master) ✗ node_modules/.bin/cqc back/* back/*/*

    Number of files: 15
    Source lines of code: 1192
    Duplicate rate: 0.84%
    High complexity rate: 0.00%
    Max complexity: 8
    ---

    是我写的规则不对嘛
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4649 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 09:48 · PVG 17:48 · LAX 02:48 · JFK 05:48
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.