1
\$\begingroup\$

My code is working correctly, but is resulting in a "Assignment branch condition too high" warning with a score of 12.08 on CodeBeat.

Codebeat is an automated code review utility that helps developers write clean code. I have been using it lately in order to monitor the quality of my code since clean code is being a necessity in today's word. Codebeat calculates the complexity of the code written and assigns a score, so it seems that the complexity of this method seems a bit high that usual. I would be grateful if someone can tell me what is complex about this method.

private void populateDictionary() {
    try {
        Files.walkFileTree(Paths.get(rootDirectory.getCanonicalPath()), EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException {
                addToDictionary(path);
                return FileVisitResult.CONTINUE;
            }
        });
    }catch(IOException e) {
        System.out.println(e.getMessage());
    }
    System.out.println(this.indexedFileContents.keySet().size() + " files read in directory " + this.rootDirectory);
}

Can anyone tell me what am I doing wrong and how I can improve my code? Thanks in advance.

\$\endgroup\$
1
  • 2
    \$\begingroup\$ Welcome to Code Review! I'm voting to close because you haven't provided enough context to understand what the code is intended to do, however I'd also like to address other close voters - as mentioned in the comments, the code is working as intended, but is being flagged by an automated review tool as being too complex. That doesn't fall under "Code not implemented or working as intended". \$\endgroup\$ Commented Oct 3, 2019 at 13:55

2 Answers 2

1
\$\begingroup\$

Well I figured out a way to refactor my code, and obtained a non-complex code by creating a method that returns the visitor named getSimpleFileVisitor(). I also created another method that will allow me to print the results that I named printFinalResults(). I have also refactored the method's name to walkFileTree(). Maybe my question was a little bit ambiguous, sorry for any inconvenience.

private void walkFileTree() {
    try{
        Files.walkFileTree(Paths.get(rootDirectory.getCanonicalPath()), EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, getSimpleFileVisitor());
    }catch(IOException e) {
        System.out.println(e.getMessage());
    }
    printFinalResults();
}

private SimpleFileVisitor<Path> getSimpleFileVisitor(){
    SimpleFileVisitor<Path> simpleFileVisitor = new SimpleFileVisitor<Path>() {
        @Override
        public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException {
            addToDictionary(path);
            return FileVisitResult.CONTINUE;
        }
    };
    return simpleFileVisitor;
}

private void printFinalResults() {
    System.out.println(this.indexedFileContents.keySet().size() + " files read in directory " + this.rootDirectory);
}
\$\endgroup\$
1
  • \$\begingroup\$ walkFileTree() still has the result of populating the dictionary; why did you change the name? Also, does Codebeat complain if you return the SimpleFileVisitor directly without assigning it to a variable? Also, consider putting printFinalResults() in a finally block. \$\endgroup\$ Commented Oct 3, 2019 at 17:49
0
\$\begingroup\$

The walkFileTree method is a sample of dependency inversion violation on method level. Instead of being given the path and the action to perform, the method is burdened with the depenendencies of finding the information itself. This is what increases the ABC value of the method. The problem is highlighted by the method having a very generic name walkFileTree but performing a very specific task instead : walking the directory beneath rootDirectory and adding the entries to a specific dictionary.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.