Java 代码中的圈复杂度为 11(最大允许为 10)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18756155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 10:46:40  来源:igfitidea点击:

Cyclomatic Complexity is 11 ( max allowed is 10 ) in Java code

java

提问by ronan

I have the following java code that violets the checkstyle saying that "Cyclomatic Complexity is 11 ( max allowed is 10 )"

我有以下 java 代码,它紫罗兰色的 checkstyle 说“圈复杂度为 11(最大允许为 10)”

 public boolean validate(final BindingResult bindingResult) {
        boolean validate = true;
        for (String channel : getConfiguredChannels()) {
            switch (channel) {
            case "SMS":
                // do nothing
                break;
            case "Email":
                // do nothing
                break;
            case "Facebook":
                // do nothing
                break;
            case "Voice":
                final SpelExpressionParser parser = new SpelExpressionParser();
                if (parser
                        .parseExpression(
                                "!voiceMessageForm.audioForms.?[audioId == '' || audioId == null].isEmpty()")
                        .getValue(this, Boolean.class)) {
                    bindingResult.rejectValue("voiceMessageForm.audioForms",
                            "message.voice.provide.all.audios");
                    validate = false;
                }
                boolean voiceContentErrorSet = false;
                    boolean voiceDescriptionErrorSet = false;
                    for (AudioForm audioForm : (List<AudioForm>) parser
                            .parseExpression(
                                    "voiceMessageForm.audioForms.?[description.length() > 8000]")
                            .getValue(this)) {
                        if (audioForm.getAddAudioBy().equals(
                                AudioForm.AddBy.TTS)
                                && !voiceContentErrorSet) {
                            voiceContentErrorSet = true;
                            bindingResult.rejectValue(
                                    "voiceMessageForm.audioForms",
                                    "message.voice.content.exceed.limit");
                        } else {
                            if (!voiceDescriptionErrorSet) {
                                voiceDescriptionErrorSet = false;
                                bindingResult
                                        .rejectValue(
                                                "voiceMessageForm.audioForms",
                                                "message.describe.voice.content.exceed.limit");
                            }
                        }
                        validate = false;
                    }
                break;
            default:
                throw new IllegalStateException("Unsupported channel: "
                        + channel);
            }
        }
        return validate;
    }
}

Please suggest a suitable way to avoid this checkstyle issue

请提出一种合适的方法来避免此检查样式问题

采纳答案by Sambuca

I'd go ahead and extract your code of the "Voice" case to another method. After that your validatemethod will look like: (You can use the refactoring tools of your IDE to do so.)

我会继续将您的“语音”案例代码提取到另一种方法。之后,您的validate方法将如下所示:(您可以使用 IDE 的重构工具来执行此操作。)

public boolean validate(final BindingResult bindingResult) {
    boolean validate = true;
    for (String channel : getConfiguredChannels()) {
        switch (channel) {
        case "SMS":
            // do nothing
            break;
        case "Email":
            // do nothing
            break;
        case "Facebook":
            // do nothing
            break;
        case "Voice":
            validate = validateVoice(bindingResult);
        default:
            throw new IllegalStateException("Unsupported channel: "
                    + channel);
        }
    }
    return validate;
}

Edit:(Added extracted method, although I did not really look into it.)

编辑:(添加了提取方法,虽然我没有真正研究它。)

private boolean validateVoice(final BindingResult bindingResult) {
    boolean validate = true;
    final SpelExpressionParser parser = new SpelExpressionParser();
    if (parser.parseExpression("!voiceMessageForm.audioForms.?[audioId == '' || audioId == null].isEmpty()").getValue(this, Boolean.class)) {
        bindingResult.rejectValue("voiceMessageForm.audioForms", "message.voice.provide.all.audios");
        validate = false;
    }
    boolean voiceContentErrorSet = false;
    boolean voiceDescriptionErrorSet = false;
    for (AudioForm audioForm : (List<AudioForm>) parser.parseExpression("voiceMessageForm.audioForms.?[description.length() > 8000]").getValue(this)) {
        if (audioForm.getAddAudioBy().equals(AudioForm.AddBy.TTS) && !voiceContentErrorSet) {
            voiceContentErrorSet = true;
            bindingResult.rejectValue("voiceMessageForm.audioForms", "message.voice.content.exceed.limit");
        } else {
            if (!voiceDescriptionErrorSet) {
                voiceDescriptionErrorSet = false;
                bindingResult.rejectValue("voiceMessageForm.audioForms", "message.describe.voice.content.exceed.limit");
            }
        }
        validate = false;
    }
    return validate;
}