3

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

请建议一种合适的方法来避免这种检查样式问题

4

1 回答 1

5

我会继续将您的“语音”案例代码提取到另一种方法。之后,您的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;
}
于 2013-09-12T05:52:05.597 回答