首页 > 解决方案 > 降低认知复杂性

问题描述

如何降低 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;
            }

        }

    }

标签: javasonarlint

解决方案


认知复杂度是衡量你的代码理解难易程度的指标。这是一种主观的可维护性检查,用于衡量嵌套的数量和存在的流中断。它与圈复杂度不同(这是逻辑的分支,需要独特的测试用例来测试该代码分支),而是非常接近的表亲。还有一些更多的静态分析可以生成认知复杂性分数。

经验法则

想想你需要编写多少个测试用例来测试你的代码。

循环量,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;
        }

    }

}

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


推荐阅读