当前位置:网站首页>How to monitor code cyclomatic complexity by refactoring indicators
How to monitor code cyclomatic complexity by refactoring indicators
2022-08-04 15:34:00 【InfoQ】
1 引言
2 背景知识
2.1 圈复杂度
2.2 Cyclomatic complexity calculation method
- 顺序结构:The order structure complexity is 1.
- if-else-else、switch-case:每增加一个分支,复杂度增加1,&& 、|| The operation is also a branch.
- 循环结构:Add a loop structure,复杂度增加1.
- return:增加一条return语句,Complexity will increase1.
2.3 Cyclomatic complexity metrics

2.4 Cyclomatic complexity reduction method
- Split into subfunctions
- Each function must have a clear function implementation,Don't merge functional implementations in pursuit of fewer lines
- Logic modules and data modules should be written separately
- Reduce unnecessary conditions、循环分支,尽量少用 if …else … ,Use a ternary expression to replaceif else
- 合并条件表达式,比如使用a || b || c
3 方案概述
3.1 脚本设计
- python
- lizard
- APScheduler
- smtplib
- pymysql

3.2 功能介绍
- C/C++ (works with C++14)
- Java
- C# (C Sharp)
- JavaScript (With ES6 and JSX)
- Python
- Golang
- List the programming languages to analyze.如果留空,All supported languages will be searched.
-l LANGUAGES, --languages LANGUAGES
- 排除与模式匹配的文件.match everything?匹配任何单个字符,“/folder/”Recursively exclude everything in a folder.可以指定多个模式.Don't forget to add around the pattern“”号.
-x EXCLUDE, --exclude EXCLUDE
- 设置白名单, 默认’./whitelizard.txt’
-W WHITELIST, --whitelist WHITELIST
- Threshold for cyclomatic complexity warnings,默认值为15,>15会产生警告.
-C CCN, --CCN CCN
- Sets the limit on the number of fields.number of lines of code,圈复杂度,令牌数,Number of parameters or custom fields.If the function setting exceeds the limit, an alarm will be issued.
-T THRESHOLDS, --Threshold THRESHOLDS
- Output to file according to format
-o OUTPUT_FILE, --output_file OUTPUT_FILE
- 通过BackgroundScheduler创建调度任务,Automatic trigger scan method,result write library
def dojob(): scheduler = BackgroundScheduler() scheduler.add_job(func, "cron", hour=21, minute=30) scheduler.start()
3.3 结果展示
3.3.1 Explanation of report nomenclature
- Cyclomatic complexity,Cyclomatic complexity is also known as branch complexity,最好保持在15 以下,The current script sets the threshold10.
- LOC,The number of lines of code that contain comments,目前设置200阀值.
- Token count ,token的个数,A program can have at most 8192 个令牌, Each token is a word,例如关键字,标识符,常量,标点符号,操作符. Counts parentheses and strings as 1 个令牌. 逗号、句点、LOCAL、分号、END and comments do not count.
- Parameter count,Parameter statistics is the number of parameters of a function,The current script sets the threshold10.
3.3.2 执行结果展示
- Windows环境运行脚本,输入file_root(文件地址)执行扫描,Supports the automatic pop-up of the browser to display the current runningHtml报告

- Execute on a weekly basis,Scan according to the system dimension,Support trigger email notification corresponding to system development to view the method name that exceeds the threshold

3.3.3 Application data monitoring
- Regularly pull the latest code of the specified branch every week,执行文件分析,存储扫描结果,Displayed through data charts

4 总结
边栏推荐
猜你喜欢
X-ray grazing incidence focusing mirror
邮差"头":{“retCode”:“999999”
性能提升400倍丨外汇掉期估值计算优化案例
remote: Check Access Error, please check your access right or username and password!fatal: Authenti
Manacher(求解最长回文子串)
李沐的深度学习笔记来了!
Online Excel based on Next.js
DevOps平台中的制品库是什么?有什么用处?
普法教育结合VR全景,直观感受和学习法治精神
leetcode: 255 Verify preorder traversal sequence binary search tree
随机推荐
What is the difference between member variable and local variable
I love Tanabata hahaha
Go 言 Go 语,一文看懂 Go 语言文件操作
numpy入门详细代码
大众点评搜索相关性技术探索与实践
Next -20- 使用自定义样式 (custom style)
为什么Redis默认序列化器处理之后的key会带有乱码?
进程间通信方式
《分布式云最佳实践》分论坛,8月11日深圳见
【Gopher 学个函数】边学边练,简单为 Go 上个分
Why, when you added a unique index or create duplicate data?
RepVGG学习笔记
GPS卫星同步时钟,NTP网络同步时钟,北斗时钟服务器(京准)
Codeforces Round #811 A~F
DocuWare平台——用于文档管理的内容服务和工作流自动化的平台(上)
C# TextBlock 上标
C端折戟,转战B端,联想的元宇宙梦能成吗?
C语言写简单三子棋
洛谷题解P4326 求圆的面积
如何优雅的消除系统重复代码?