我有以下java代码违反checkstyle说" Cyclomatic Complexity是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;
}
}
Run Code Online (Sandbox Code Playgroud)
请建议一种合适的方法来避免这种checkstyle问题
我将继续将"Voice"案例的代码提取到另一种方法.之后,您的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;
}
Run Code Online (Sandbox Code Playgroud)
编辑:(添加提取的方法,虽然我没有真正研究它.)
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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10579 次 |
| 最近记录: |