如何降低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;
}
}
}
答案 0 :(得分:3)
认知复杂度衡量您的代码的理解难度。这是一种主观的可维护性检查,用于测量嵌套的数量,以及存在的流程中断。它与圈复杂度(它是逻辑的分支,需要独特的测试用例来测试该代码分支)不同,而是非常接近的表亲。还有一些静态分析可以用于生成认知复杂性分数。
经验法则
考虑您需要编写多少个测试用例来测试您的代码段。
循环次数,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;
}
}
}
对其进行伪代码以找出您想如何在插入循环中编写方法。