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

标签 java

我有以下 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;
    }
}

请提出一个合适的方法来避免这个 checkstyle 问题

最佳答案

我会继续将“语音”案例的代码提取到另一个方法。之后,您的 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;
}

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

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

关于java - Java 代码中的圈复杂度为 11(最大允许值为 10),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18756155/

相关文章:

java - 无法读取java中的继承值

java - 如何在 TensorFlowInferenceInterface 中使用 feed 和 fetch 函数?

openssl aes-256-ctr加密文件Java解密

java - 加载-操作-更新图片/像素的最佳方法是什么?

java - 使用 Java 和 sbt 依赖项进行 Akka 编程

java - 从图表模型生成 Java 代码

java - 无法启动sonarqube 6.1

java - 如何在 IntelliJ IDEA 的 Web 应用程序项目中部署 Maven 依赖项?

java - 哪种数据结构更适合使用顺序搜索来搜索元素 - 数组列表还是链接列表?

java - ConcurrentModificationException Netbeans GUI