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

《分布式云最佳实践》分论坛,8月11日深圳见

leetcode: 250. Count subtrees of equal value

Why, when you added a unique index or create duplicate data?

365天挑战LeetCode1000题——Day 049 非递增顺序的最小子序列 贪心

X-ray grazing incidence focusing mirror

直播回放含 PPT 下载|基于 Flink & DeepRec 构建 Online Deep Learning

一文解答DevOps平台的制品库是什么

Byte、Short、Integer、Long内部缓存类的对比与源码分析

关于pnpm包管理器的版本问题

第三章 Scala运算符
随机推荐
如何优雅的消除系统重复代码?
HarePoint Analytics for SharePoint Online
使用百度EasyDL实现森林火灾预警识别
leetcode: 255 Verify preorder traversal sequence binary search tree
IP第十七天笔记
从-99打造Sentinel高可用集群限流中间件
附加:自定义注解(参数校验注解);(写的不好,别看…)
C# 谁改了我的代码
qt 复杂界面信号槽设计
卖家寄卖流程梳理
性能提升400倍丨外汇掉期估值计算优化案例
剑指Offer 63.股票的最大利润
Resharper 如何把类里的类移动到其他文件
RepVGG学习笔记
普法教育结合VR全景,直观感受和学习法治精神
C# 将dll打包到程序中
SAP ABAP SteamPunk 蒸汽朋克的最新进展 - 嵌入式蒸汽朋克
H5 开发内嵌页面跨域问题
保证通信的机制有哪些
Byte、Short、Integer、Long内部缓存类的对比与源码分析