降低认知复杂性

Reducing Cognitive complexity

如何降低 while 循环中 if 语句的认知复杂性?这是用于将节点插入线程二叉搜索树的代码。这是我的操作逻辑,但 ide 给了我这些问题。我该如何修复它们?

Refactor this method to reduce its Cognitive Complexity from 18 to the 15 allowed. [+11 locations]

Reduce the total number of break and continue statements in this loop to use at most one. [+2 locations]

这是代码

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;
        }

    }

}

对其进行伪编码以确定您希望如何在插入循环中编写方法。