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