当前位置:网站首页>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 总结
边栏推荐
猜你喜欢

全球电子产品需求放缓,三星手机越南工厂每周只需要干 3~4 天

实战:10 种实现延迟任务的方法,附代码!

卖家寄卖流程梳理

ICDE‘22推荐系统论文之Research篇

动态数组底层是如何实现的

【北亚数据恢复】IBM System Storage存储lvm信息丢失,卷访问不了的数据恢复方案

Semaphore 基本原理

【已解决】allure无法生成json文件和AttributeError: module ‘allure‘ has no attribute ‘severity_level‘

HarePoint Analytics for SharePoint Online

Xi'an Zongheng Information × JNPF: Adapt to the characteristics of Chinese enterprises, fully integrate the cost management and control system
随机推荐
聊聊与苹果审核员的爱恨情仇
《电磁兼容防护EMC》学习笔记
PHP 图片转PDF
Redis-哨兵模式
IP报文头解析
普法教育结合VR全景,直观感受和学习法治精神
HarePoint Analytics for SharePoint Online
使用百度EasyDL实现森林火灾预警识别
【Harmony OS】【FAQ】Hongmeng Questions Collection 2
Game network UDP + FEC + KCP
tif转mat
remote: Check Access Error, please check your access right or username and password!fatal: Authenti
MySQL优化学习笔记
技术分享| 小程序实现音视频通话
2022杭电多校3
GPS卫星同步时钟,NTP网络同步时钟,北斗时钟服务器(京准)
界面组件DevExpress ASP.NET Core v22.1 - 增强数据导出功能
进程间通信方式
Redis-哨兵模式
MySQL当前读、快照读、MVCC