【问题标题】:java, sonar, Cyclomatic Complexityjava,声纳,圈复杂度
【发布时间】:2018-04-23 14:02:50
【问题描述】:

任何人都可以帮助我将以下方法的 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 ,因为它也会导致声纳问题。 对我有很大的帮助

【问题讨论】:

  • 只需将其分解为几个较小的方法。你有没有尝试过?

标签: java sonarqube complexity-theory developer-tools


【解决方案1】:

您可以将部分代码提取为方法和/或重构一些可以以其他方式使用的变量。此外,当您让 cmets 解释您的代码时,这强烈表明您的逻辑可以在那里改进:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-14
    • 2020-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-29
    相关资源
    最近更新 更多