当前位置:网站首页>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);
边栏推荐
- 【深度学习】:《PyTorch入门到项目实战》第九天:Dropout实现(含源码)
- MySQL CDC if the binlog log file is incomplete, can you read all the data in the full volume stage
- 技术分享 | 误删表以及表中数据,该如何恢复?
- 快速掌握 Kotlin 集合函数
- 有趣的 Kotlin 0x08:What am I
- Call DLL file without source code
- Binary representation of negative integers and floating point numbers
- 阿里大哥教你如何正确认识关于标准IO缓冲区的问题
- 做题笔记2(两数相加)
- parseJson
猜你喜欢

Leetcode learn to insert and sort unordered linked lists (detailed explanation)

Nowcode- learn to delete duplicate elements in the linked list (detailed explanation)

Leetcode learn complex questions with random pointer linked lists (detailed explanation)

阿里云 MSE 支持 Go 语言流量防护
![[deep learning]: day 5 of pytorch introduction to project practice: realize softmax regression from 0 to 1 (including source code)](/img/19/18d6e94a1e0fa4a75b66cf8cd99595.png)
[deep learning]: day 5 of pytorch introduction to project practice: realize softmax regression from 0 to 1 (including source code)

Brother Ali teaches you how to correctly understand the problem of standard IO buffer

ERROR: transport library not found: dt_ socket

Probability theory and mathematical statistics Chapter 1

【深度学习】:《PyTorch入门到项目实战》:简洁代码实现线性神经网络(附代码)

【深度学习】:《PyTorch入门到项目实战》第四天:从0到1实现logistic回归(附源码)
随机推荐
Leetcode learn to insert and sort unordered linked lists (detailed explanation)
Re12:读论文 Se3 Semantic Self-segmentation for Abstractive Summarization of Long Legal Documents in Low
Applet: get element node information
有趣的 Kotlin 0x09:Extensions are resolved statically
Implementation of transfer business
Re13:读论文 Gender and Racial Stereotype Detection in Legal Opinion Word Embeddings
MySQL安装教程
Probability theory and mathematical statistics Chapter 1
Question making note 2 (add two numbers)
Rsync service deployment and parameter details
ticdc同步数据怎么设置只同步指定的库?
Binary representation of negative integers and floating point numbers
Cluster construction and use of redis5
Alibaba cloud MSE supports go language traffic protection
Installation of QT learning
【深度学习】:《PyTorch入门到项目实战》第五天:从0到1实现Softmax回归(含源码)
Huawei mate 40 series exposure: large curvature hyperboloid screen, 5nm kylin 1020 processor! There will also be a version of Tianji 1000+
Exercise note 5 (square of ordered array)
Record the processing process of CEPH two RBDS that cannot be deleted
Semtech launched Lora edge, a geolocation solution for the Internet of things, and the first chip lr1110 is now on the market