java - 降低认知复杂性
问题描述
如何降低 while 循环中 if 语句的认知复杂性?这是用于将节点插入线程二叉搜索树的代码。这是我的操作逻辑,但 ide 给了我这些问题。我该如何修复它们?
重构此方法以将其认知复杂度从 18 降低到允许的 15。[+11 个地点]
减少此循环中的 break 和 continue 语句总数,最多使用一个。[+2 个地点]
这是代码
public class threaded {
class Node{
Node left;
boolean lthread;
int data;
boolean rthread;
Node right;
Node(int data){
left = null;
lthread = true;
this.data = data;
rthread = true;
right = null;
}
}
Node root = null;
void insert(int data){
Node ptr = root;
Node par = null; // parent of the node to be inserted
while (ptr != null){
if (data == ptr.data){
System.out.println("Duplicate key");
return;
}
par = ptr;
if (data < ptr.data){
if (!ptr.lthread){
ptr = ptr.left;
}
else{
break;
}
}
else {
if (!ptr.rthread){
ptr = ptr.right;
}
else{
break;
}
}
Node tmp = new Node(data); // creating a new node
if (root == null){
root = new Node(data);
}
else if (data < par.data){
tmp.left = par.left;
tmp.right = par;
par.lthread = false;
par.left = tmp;
}
else if (data > par.data){
tmp.left = par;
tmp.right = par.right;
par.rthread = false;
par.right = tmp;
}
}
}
解决方案
认知复杂度是衡量你的代码理解难易程度的指标。这是一种主观的可维护性检查,用于衡量嵌套的数量和存在的流中断。它与圈复杂度不同(这是逻辑的分支,需要独特的测试用例来测试该代码分支),而是非常接近的表亲。还有一些更多的静态分析可以生成认知复杂性分数。
经验法则
想想你需要编写多少个测试用例来测试你的代码。
循环量,if/else,切换到自己的方法
if
else // complexity 2 there can be two paths for this methods
if
if
else
else // complexity 4 there are four paths for this method
现在...结合这两者...您有多达 8 个条件来测试您的所有代码路径!
在测量任何事物的复杂性时。想想你需要编写多少测试来测试该方法。您可以通过将复杂性推迟到较小的单元方法来降低认知复杂性。
关于您的实施的一些评论,insert
以帮助您的旅程
void insert(int data){
Node ptr = root;
Node par = null; // parent of the node to be inserted
while (ptr != null){
if (data == ptr.data){
System.out.println("Duplicate key");
return;
}
par = ptr;
if (data < ptr.data){
if (!ptr.lthread){
ptr = ptr.left;
}
else{ // Exit condition can you handle this in your loop conditional?
break;
}
}
else {
if (!ptr.rthread){
ptr = ptr.right;
}
else{
break;
}
}
Node tmp = new Node(data); // creating a new node
if (root == null){
root = new Node(data);
}
else if (data < par.data){ // Can you do this in a separate method? -- you might even be able to combine it with your conditions above.
tmp.left = par.left;
tmp.right = par;
par.lthread = false;
par.left = tmp;
}
else if (data > par.data){ // Can you do this in a separate method? append()? if you combine it with your conditional above -- you can go appendLeft(), appendRight()
tmp.left = par;
tmp.right = par.right;
par.rthread = false;
par.right = tmp;
}
}
}
对其进行伪编码以弄清楚您希望如何在插入循环中编写方法。
推荐阅读
- deserialization - 如何解析从 Presto 返回的 P4HYPERLOGLOG?
- c# - 如何更改网格中的行/列背景颜色?
- c# - 我可以为我的 .NET Core Web 应用程序中的不同站点附加身份验证 cookie 吗?
- flutter - 如何在 Flutter 中发送和/或接收颜色作为参数
- javascript - 如何更改选择列表js中的选项
- php - 在php中合并两个键控数组
- asp.net-mvc - 如何在不重新加载页面的情况下使用 BeginForm 发布到 MVC 表单
- java - 如何从 32 位 RGB 值中获取单一颜色?
- c++ - 如何使类 Forum 使用 C++ 中的字符串?
- javascript - 根据指定长度的元素创建新 div 的最佳方法?