当前位置:网站首页>Reduce cycle complexity
Reduce cycle complexity
2022-07-28 17:03:00 【Zhengx Hui】
Reduce cycle complexity
demand :
Code optimization : Recently, the company required to rectify the code , The cyclomatic complexity is involved . I have been exposed to time complexity before , So I have to search a wave of information .
sketch :
1. Concept :
Cyclomatic complexity (Cyclomatic complexity,CC) Also known as conditional complexity , It's a measure of code complexity , Its symbol is V(G). In short , That is, count the number of conditional judgments in the code .
2. Influencing factors :
- if sentence
- while sentence
- for sentence
- case sentence
- catch sentence
- and and or Boolean operation
- ? : Ternary operator
3. Calculation method :
**V (G) ** =P +1
P Represents the number of conditional judgments in the code , That is, the number of influencing factors above ,1 Cycle complexity representing normal order
example :
The complexity of the next cycle is 2:
while ("0".equals(a)){
if (b != null){
c++;
}
}
4. Be careful :
- if (a == null && b != null) The cycle complexity is 2, Because there are two judgments , So when you plan to put two if When combining conditions , The cycle complexity has not changed , What changes is cognitive complexity .
- if-else and if The cyclomatic complexity is 1, Don't think about leaving only if Get rid of else, The results have not changed
3. Conditional nesting and presentation do not affect the calculation of cycle complexity , For example, put if Inside for Cycle to the outside , As a result, the cyclomatic complexity is still 2
4. if Conversion to ternary operators does not affect cyclomatic complexity
5. The purpose of reducing cycle complexity is to optimize code , Reduce maintenance costs , Don't force code extraction to complicate in order to reduce
6. Never change the original logic
An optimization method :
At present, only a few methods are used , Here are some optimization examples
Try to use encapsulated util class
// If the string is empty // Before the change : if (a == null || a.length() == 0){ b ++; } // After reform : if (StringUtil.isEmpty()){ b ++; }// Set empty judgment (List\Map\Set) // Before the change : if (list == null || list.size() == 0){ b ++; } // After reform : if (CollectionUtils.isEmpty){ b ++; }// Close various streams // Before the change : if (stream != null){ try { stream.close(); } catch (IOException e) { e.printStackTrace(); } } // After reform : IOUtils.closeQuietly(stream)switch/case Turn into map
// Before the change : switch (a){ case 1: b = "f"; break; case 2: b = "g"; break; default: break; } // After reform : Map<Integer,String> map = new HashMap<>(); map.put(1,"f"); map.put(2,"g"); b = map.get(a);Extract Method
// Before the change : if ( b == null ){ a = 1; }else{ a= 4; } // After reform : public static <T> T getValueByCondition(boolean condition,T valueOfTrue,T valueOfFalse){ if (condition){ return valueOfTrue; }else { return valueOfFalse } } a = getValueByCondition(b == null,1,4);
边栏推荐
- 做题笔记2(两数相加)
- Rsync service deployment and parameter details
- Exercise note 5 (square of ordered array)
- 【深度学习】:《PyTorch入门到项目实战》第八天:权重衰退(含源码)
- 技术分享 | MySQL Shell 定制化部署 MySQL 实例
- go语言慢速入门——流程控制语句
- In 2020q2, shipments in the global tablet market soared by 26.1%: Huawei ranked third and Lenovo increased the most!
- Question making note 2 (add two numbers)
- Quickly master kotlin set functions
- RE14: reading paper illsi interpretable low resource legal decision making
猜你喜欢

技术分享 | 误删表以及表中数据,该如何恢复?

Interesting kotlin 0x08:what am I

Cluster construction and use of redis5

Add differential pairs and connections in Ad

小程序:scroll-view默认滑倒最下面

有趣的 Kotlin 0x08:What am I

Tcp/ip related

egg(十九):使用egg-redis性能优化,缓存数据提升响应效率
![[deep learning]: day 4 of pytorch introduction to project practice: realize logistic regression from 0 to 1 (with source code)](/img/19/18d6e94a1e0fa4a75b66cf8cd99595.png)
[deep learning]: day 4 of pytorch introduction to project practice: realize logistic regression from 0 to 1 (with source code)

MySQL安装教程
随机推荐
阿里云-武林头条-建站小能手争霸赛
MySQL5.7及SQLyogV12安装及使用破解及常用命令
RE14: reading paper illsi interpretable low resource legal decision making
关于Bug处理的一些看法
Huawei mate 40 series exposure: large curvature hyperboloid screen, 5nm kylin 1020 processor! There will also be a version of Tianji 1000+
MySQL安装教程
MD5 encryption verification
小程序:获取元素节点信息
asmlinkage的理解
Leetcode9. Palindromes
【JS】1394- ES2022 的 8 个实用的新功能
Exercise note 5 (square of ordered array)
mysql cdc 如果binlog日志文件不全,全量阶段能读到所有数据吗
做题笔记4(第一个错误的版本,搜索插入位置)
Alibaba cloud - Wulin headlines - site building expert competition
Text filtering skills
Semtech launched Lora edge, a geolocation solution for the Internet of things, and the first chip lr1110 is now on the market
3D建模工具Archicad 26全新发布
Introduction and implementation of stack (detailed explanation)
[deep learning]: day 6 of pytorch introduction to project practice: multi-layer perceptron (including code)