ronan
ronan

Reputation: 4672

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

I have the following java code that violets the checkstyle saying that "Cyclomatic Complexity is 11 ( max allowed is 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

Upvotes: 5

Views: 24204

Answers (1)

Sambuca
Sambuca

Reputation: 1254

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

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

Upvotes: 6

Related Questions