Refactor this method to reduce its Cognitive Complexity from 19 to the 15 allowed

Hi all, I’m Andrea and i’m a computer science student in university. I’m doing quality improving of a code given by my teacher, and using SonarCloud, Maven, Redmine. On RedMine, i have this issue to fix:

Refactor this method to reduce its Cognitive Complexity from 19 to the 15 allowed

And the piece of code about this is issue is this

       @Override
    @SuppressWarnings("unchecked")
    public IArtNode<V> remove(long key, int level) {

//        String prefix = StringUtils.repeat(" ", (56 - level) / 4);
//        log.debug(prefix + " ------ REMOVE {}", String.format("%X", key));
        //          56 48 40 32 24 16  8  0
        // rem key  00 00 11 22 33 44 55 66

//        log.debug(prefix + "level={} nodeLevel={}", level, nodeLevel);
//        log.debug(prefix + "key={} nodeKey={}", key, nodeKey);


        if (level != nodeLevel && ((key ^ nodeKey) & (-1L << (nodeLevel + 8))) != 0) {
            return this;
        }
        final short nodeIndex = (short) ((key >>> nodeLevel) & 0xFF);
        Object node = null;
        int pos = 0;
        while (pos < numChildren) {
            if (nodeIndex == keys[pos]) {
                // found
                node = nodes[pos];
                break;
            }
            pos++;
        }

        if (node == null) {
            // not found
            return this;
        }

        // removing
        if (nodeLevel == 0) {
            removeElementAtPos(pos);
        } else {
            final IArtNode<V> resizedNode = ((IArtNode<V>) node).remove(key, nodeLevel - 8);
            if (resizedNode != node) {
                // TODO put old into the pool
                // update resized node if capacity has decreased
                nodes[pos] = resizedNode;
                if (resizedNode == null) {
                    removeElementAtPos(pos);
                    if (numChildren == 1) {
//                        log.debug(prefix + "CAN MERGE! nodeLevel={} level={}", nodeLevel, level);
                        // todo put 'this' into pul
                        IArtNode<V> lastNode = (IArtNode<V>) nodes[0];
                        //   lastNode.setNodeLevel(nodeLevel);
                        return lastNode;
                    }
                }
            }
        }

        if (numChildren == 0) {
            // indicate if removed last one
            Arrays.fill(nodes, null);
            objectsPool.put(ObjectsPool.ART_NODE_4, this);
            return null;
        } else {
            return this;
        }
    }

I’ve tried to create methods for every if in this code, but i can’t reach to any result. Any other suggestions?

Hi Andrea,

I guess you’ve got an integration that creates Redmine issues for each SonarCloud issue? You’ll do better working in SonarLint and looking directly at SonarCloud.

SonarLint/SonarCloud will tell you which lines/structures contribute to the overall method total. From there it should be clearer how to refactor.

 
HTH,
Ann