当前位置:网站首页>Sonar:cognitive complexity
Sonar:cognitive complexity
2022-07-07 12:01:00 【Peipei Dad】
background
When scanning the code , Discover the use of sonar, Will prompt “Refactor this method to reduce its Cognitive Complexity from 31 to 15 allowed And so on .
Translated into Chinese is : refactoring , To reduce the cognitive complexity of code from 31 drop to 15.
It's easy to understand , The cognitive complexity of the current code is a little high , Not friendly to reading , It is recommended to refactor its code .
Cognitive Complexity
Cognitive Complexity Translating into Chinese is cognitive complexity .
In short, it is a measure of the complexity of the code being read and understood , The code is fixed The method is transformed into a numerical value of complexity .
The higher the cognitive complexity value , Explain that the more complex this code is !
Scoring example
Here is a very useful example , We can point out the problem of cyclomatic complexity . The following two methods have the same cycle complexity , But the difficulty of understanding is very poor :

Simple rules of cognitive complexity
- Some syntax sugar is used in the code , Reduce many sentences to one sentence , however The code doesn't get any more complicated
- appear "break" Stop linear code reading comprehension , If a cycle occurs 、 Conditions 、try-catch、switch-case、 A string of and or The operator 、 recursive , as well as goto Class jump , therefore The code is therefore more complex
- The multi-level nested structure makes The code is therefore more complex
Cognitive complexity scoring rules
Ignore abbreviations : Abbreviate multiple sentences of code into one readable code , Do not change the difficulty of understanding ;
In the formulation of cognitive complexity , A guiding principle is : Encourage users to write good coding specifications . in other words , You need to ignore or underestimate what makes the code more readable feature( Do not count into complexity ).
“ Method ” Itself is a simple example , Take apart a piece of code and separate a few sentences into a method , Replace with a method call ,“ Abbreviation ” it , Cognitive complexity will not increase because of this method call .
alike , Cognitive complexity will also be ignored null-coalescing The operator ,x?.myObject Such operators do not add complexity , Because these operations also abbreviate multiple pieces of code into one
In linear code logic , There is something that breaks the logic , difficulty +1;
In the formulation of cognitive complexity , Another guiding principle is : Structural control will interrupt a linear flow from beginning to end , It takes code maintainers more effort to understand the code . On the premise that this will lead to additional burden , Cognitive complexity is assessed as increasing by Structural Class complexity :
loop : for, while, do while, ...
Conditions : Ternary operator , if, #if, #ifdef...
in addition , The following accounting department Hybrid Class complexity :
else if, elif, else, ...
But not included Nesting Class complexity , Because this quantity is before the calculation if Statement has been counted .
These add complexity , In fact, it is very similar to the calculation method of cycle complexity , But the extra , Cognitive complexity is also calculated
When what breaks logic is a nest , difficulty +1;
Compared with five structures nested consecutively , Five linearly continuous if\for The structure is much better understood . Because such nesting will increase the cost of understanding code , Therefore, cognitive complexity will be regarded as a Nesting Class complexity increases .
Specially , Every time one leads to Structural Class or Hybrid Class complex structure , When another structure is nested , Each layer of nesting should be added again Nesting Class complexity .
Further said that , Complexity scores are derived from the following different types :
A. Nesting: Nesting one piece of code logic into another ;
B. Structural: Nested control flow structure ;
C. Fundamental: Statements that are not affected by nesting ;
D. Hybrid: Some control flow structures , But it is not included in the nesting ;
However, there is no mathematical difference between different types , It's all about adding one to the complexity . Distinguish between the different categories to be calculated , It is easier to know whether nested logic is applicable at a certain place . The following sections will further elaborate on these rules and the rationale behind them .
If you have a better interest in it , You can see the official introduction document :https://www.sonarsource.com/docs/CognitiveComplexity.pdf
边栏推荐
- 【纹理特征提取】基于matlab局部二值模式LBP图像纹理特征提取【含Matlab源码 1931期】
- [neural network] convolutional neural network CNN [including Matlab source code 1932]
- Basic introduction to the 16 tabs tab control in the fleet tutorial (the tutorial includes source code)
- Common locking table processing methods in Oracle
- Flet tutorial 17 basic introduction to card components (tutorial includes source code)
- Flet教程之 18 Divider 分隔符组件 基础入门(教程含源码)
- R语言使用quantile函数计算评分值的分位数(20%、40%、60%、80%)、使用逻辑操作符将对应的分位区间(quantile)编码为分类值生成新的字段、strsplit函数将学生的名和姓拆分
- Swiftui tutorial how to realize automatic scrolling function in 2 seconds
- Detailed explanation of debezium architecture of debezium synchronization
- R language uses the quantile function to calculate the quantile of the score value (20%, 40%, 60%, 80%), uses the logical operator to encode the corresponding quantile interval (quantile) into the cla
猜你喜欢

Solve the problem that vscode can only open two tabs

HCIA复习整理

Up meta - Web3.0 world innovative meta universe financial agreement

<No. 8> 1816. 截断句子 (简单)

Flet教程之 14 ListTile 基础入门(教程含源码)
![111.网络安全渗透测试—[权限提升篇9]—[Windows 2008 R2内核溢出提权]](/img/2e/da45198bb6fb73749809ba0c4c1fc5.png)
111.网络安全渗透测试—[权限提升篇9]—[Windows 2008 R2内核溢出提权]
![[system design] index monitoring and alarm system](/img/8e/9c4c168f7f2b8e1f0786a5fe158544.png)
[system design] index monitoring and alarm system

About how to install mysql8.0 on the cloud server (Tencent cloud here) and enable local remote connection

Talk about SOC startup (VI) uboot startup process II

Programming examples of stm32f1 and stm32subeide -315m super regenerative wireless remote control module drive
随机推荐
相机标定(2): 单目相机标定总结
Detailed explanation of debezium architecture of debezium synchronization
竟然有一半的人不知道 for 与 foreach 的区别???
正在运行的Kubernetes集群想要调整Pod的网段地址
Onedns helps college industry network security
Automated testing framework
【最短路】ACwing 1127. 香甜的黄油(堆优化的dijsktra或spfa)
总结了200道经典的机器学习面试题(附参考答案)
There are so many factors that imprison you
【滤波跟踪】捷联惯导纯惯导解算matlab实现
[neural network] convolutional neural network CNN [including Matlab source code 1932]
千人规模互联网公司研发效能成功之路
Swiftui swift internal skill how to perform automatic trigonometric function calculation in swift
112.网络安全渗透测试—[权限提升篇10]—[Windows 2003 LPK.DDL劫持提权&msf本地提权]
.NET MAUI 性能提升
How much do you know about excel formula?
通过环境变量将 Pod 信息呈现给容器
zero-shot, one-shot和few-shot
问下flinkcdc2.2.0的版本,支持并发,这个并发是指多并行度吗,现在发现,mysqlcdc全
How to write test cases for test coupons?