简体   繁体   中英

java, sonar, Cyclomatic Complexity

can anyone help me to reduce cylomatic complexity for below method upto 10..also considering no nesting of if else is allow as it will also cause sonar issue. It will be great help for me

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);
            }
        }
    }

can anyone help me to reduce cylomatic complexity for below method upto 10..also considering no nesting of if else is allow as it will also cause sonar issue. It will be great help for me

You could extract part of your code to methods and/or refactor some variables which could be used in other way. Also, when you have comments explaining your code it is a strong indicator that your logic can be improved there:

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);
}

Doing this your code will be more readable, you will avoid useless variables using logic and your cyclomatic complexity will decrease.

For more tips, I recommend access refactoring.guru .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM