java,声纳,Cyclomatic Complexity

时间:2018-04-23 14:02:51

标签: java sonarqube complexity-theory developer-tools

任何人都可以帮助我降低以下方法的cylomatic复杂度高达10 ..同时考虑到if else的嵌套不允许因为它也会导致声纳问题。 这对我很有帮助

private void processIntransitFile(String fileName) {
        if (StringUtils.isNotBlank(fileName))
            return;

        // read Intransit folder and do the processing on these files
        try (BufferedReader bufferedReader = new BufferedReader(new FileReader(intransitDir + fileName))) {
            TokenRangeDTO tokenRangeDTO = new TokenRangeDTO();
            int count = 0;
            String header = "";
            String next;
            String line = bufferedReader.readLine();
            LinkedHashSet<String> tokenRanges = new LinkedHashSet<>();
            int trCount = 0;
            boolean first = true;
            boolean last = line == null;
            while (!last) {
                last = (next = bufferedReader.readLine()) == null;
                if (!first && !last) {
                    tokenRanges.add(line);
                }
                // read first line of the file
                else if (first && line.startsWith(H)) {
                    header = line;
                    first = false;
                } else if (first && !line.startsWith(H)) {
                    tokenRangeDTO.setValidationMessage(HEADER_MISSING);
                    first = false;
                }
                // read last line of the file
                else if (last && line.startsWith(T)) {
                    trCount = getTrailerCount(tokenRangeDTO, line, trCount);
                } else if (last && !line.startsWith(T)) {
                    tokenRangeDTO.setValidationMessage(TRAILOR_MISSING);
                }

                line = next;
                count++;
            }
            processInputFile(fileName, tokenRangeDTO, count, header, tokenRanges, trCount);
        } catch (IOException e) {
            LOGGER.error(IO_EXCEPTION, e);
        } catch (Exception e) {
            LOGGER.error("Some exception has occured", e);
        } finally {
            try {
                FileUtils.deleteQuietly(new File(intransitDir + fileName));
            } catch (Exception ex) {
                LOGGER.error(STREAM_FAILURE, ex);
            }
        }
    }

任何人都可以帮助我降低以下方法的cylomatic复杂度高达10 ..同时考虑到if else的嵌套不允许因为它也会导致声纳问题。 这对我很有帮助

1 个答案:

答案 0 :(得分:0)

您可以将部分代码提取到方法和/或重构某些可能以其他方式使用的变量。此外,当您有解释代码的注释时,它是一个强有力的指标,可以改善您的逻辑:

private void processIntransitFile(String fileName) {
    if (StringUtils.isNotBlank(fileName)) return;

    processFromIntransitDirectory(fileName);
}

private void processFromIntransitDirectory(String fileName) {
    try (BufferedReader bufferedReader = new BufferedReader(getFileFromIntransitFolder(fileName))) {
        TokenRangeDTO tokenRangeDTO = new TokenRangeDTO();
        int count = 0;
        String header = "";
        String next;
        String line = bufferedReader.readLine();
        LinkedHashSet<String> tokenRanges = new LinkedHashSet<>();
        int trCount = 0;
        while (!isLastLine(line)) {
            next = bufferedReader.readLine();
            if (!isFirstLine(count) && !isLastLine(next)) {
                tokenRanges.add(line);
            }
            header = readFirstLine(line, count, tokenRangeDTO);
            trCount = readLastLine(line, next, trCount, tokenRangeDTO);

            line = next;
            count++;
        }
        processInputFile(fileName, tokenRangeDTO, count, header, tokenRanges, trCount);
    } catch (IOException e) {
        LOGGER.error(IO_EXCEPTION, e);
    } catch (Exception e) {
        LOGGER.error("Some exception has occured", e);
    } finally {
        try {
            FileUtils.deleteQuietly(new File(intransitDir + fileName));
        } catch (Exception ex) {
            LOGGER.error(STREAM_FAILURE, ex);
        }
    }
}

private boolean isLastLine(String line) {
    return line != null;
}

private String readFirstLine(String line, int count, TokenRangeDTO tokenRangeDTO) {
    if (isFirstLine(count) && isHeader(line)) {
        return line;
    } else if (isFirstLine(count) && !isHeader(line)) {
        tokenRangeDTO.setValidationMessage(HEADER_MISSING);
    }
    return StringUtils.EMPTY;
}

private int readLastLine(String line, String next, int trCount, TokenRangeDTO tokenRangeDTO){
    if (isLastLine(next) && isTrailor(line)) {
        return getTrailerCount(tokenRangeDTO, line, trCount);
    } else if (last && !isTrailor(line)) {
        tokenRangeDTO.setValidationMessage(TRAILOR_MISSING);
    }

    return 0;
}

private boolean isTrailor(String line) {
    return line.startsWith(T);
}

private boolean isHeader(String line) {
    return line.startsWith(H);
}

private boolean isFirstLine(int count) {
    return count == 0;
}

private FileReader getFileFromIntransitFolder(String fileName) {
    return new FileReader(intransitDir + fileName);
}

这样做你的代码将更具可读性,你将避免使用逻辑的无用变量,你的圈复杂度会降低。

有关更多提示,建议您访问refactoring.guru