Pmd告诉我这个方法(thirdRowsValidation)的复杂度为14,但我无法达到减少代码的模式。
public void thirdRowsValidation(String thirdRowCsv) {
String[] lines1 = thirdRowCsv.split(",");
for (int i = 0; i < lines1.length; i++) {
if (indexBookngEnd == i && "".equals(temporalValidateBookngEnd)) {
temporalValidateBookngEnd = (" to " + lines1[i] + "\n");
}
if (indexBookngStart == i
&& !("".equals(temporalValidateBookngEnd))) {
finalOutput.append("Then it should have booking window ");
indexBookngStart = -1;
}
if (indexTravelEnd == i && "".equals(temporalValidateTravelEnd)) {
temporalValidateTravelEnd = (" to " + lines1[i] + "\n");
}
if (indexTravelStart == i
&& !("".equals(temporalValidateTravelEnd))) {
finalOutput.append("Then it should have travel window ");
String idHeaderColumn = String.format("%1$-" + 5 + "s", "id");
String typeHEaderColumn = String.format("%1$-" + 50 + "s","type");
finalOutput.append("| ");
finalOutput.append(idHeaderColumn);
indexTravelStart = -1;
}
if (indexPackageDescription == i) {
temporalPackages = String.format("%1$-" + 50 + "s", lines1[i]);
}
if (indexPackageCode == i
&& !(lines1[i].matches("[+-]?\\d*(\\.\\d+)?"))
&& indexTravelStart == -1) {
finalOutput.append("| ");
}
}
}
参考1:
public void secondRowValidation(String secondRowCsv) {
String[] lines1 = secondRowCsv.split(",");
for (int i = 0; i < lines1.length; i++) {
if ("Bookng start".equalsIgnoreCase(lines1[i])) {
indexBookngStart = i;
}
if ("Bookng end".equalsIgnoreCase(lines1[i])) {
indexBookngEnd = i;
}
来自\ n及更高版本的数组,用于“,”
public String getStoryFromCsv(String convert) {
String[] lines = convert.split("(\n)");
for (int j = 0; j < lines.length; j++) {
arrayPerRow = lines[j];
if (j == 0) { // get marketing type and number
firstRowValidation(arrayPerRow);
}
if (j == 1) { // get headers
secondRowValidation(arrayPerRow);
}
if (j > 1) { // get info and append according to headers
thirdRowsValidation(arrayPerRow);
}
}
所以我拥有的是: - 方法thirdRowsValidation()的NPath复杂度为649 - 方法'thirdRowsValidation'的Cyclomatic Complexity为14。
最后,我正在达到这样的文字只是因为你们有一个想法:
Then it should have booking window 8/8/16 to 10/8/16
Then it should have travel window 11/6/16 to 12/25/16
And it should have online packages:
| id | type |
| 34534 | aaa Pkg |
| D434E | MKW Pkg + asdasdasdasdasdasdas |
| F382K | sds Pkg + Ddding |
| X582F | OYL Pkg + Deluxe Dining |
答案 0 :(得分:2)
该方法的复杂性很高,因为它做了很多不同的事情。尝试每种方法做一件事。
public String getStoryFromCsv(String csv) {
StringBuilder story = new StringBuilder();
String[] lines = csv.split("\n”);
appendBookingWindowValidation(story, lines[0]);
appendTravelWindowValidation(story, lines[1]);
appendOnlinePackageValidation(story, lines);
return story.toString();
}
private void appendBookingWindowValidation(StringBuilder story, String firstLine) {
story.append("Then it should have booking window ");
// extract start and end date from 'firstLine'
// and append them to the story
}
private void appendTravelWindowValidation(StringBuilder story, String secondLine) {
story.append("Then it should have travel window ");
// extract start and end date from 'secondLine'
// and append them to the story
}
private void appendOnlinePackageValidation(StringBuilder story, String[] lines) {
story.append("And it should have online packages:\n")
.append("| id | type |\n");
for (int i = 2 ; i < lines.length; i++) {
// create and append a row of the online packages table
}
}
尝试将方法所需的所有内容作为其参数之一传递。方法不应该依赖于在不同方法中设置的字段的值。这样可以降低复杂性,并使代码更易于阅读,理解和测试。
如果某个方法或类具有高复杂性,那么通常意味着它会尝试同时执行太多不同的操作。退后一步,尝试识别它所做的不同事情并分别实施。这将自动导致代码的复杂性低。
将内部循环提取到新方法中通常是一种快速入侵,有助于降低单个方法的复杂性,但不会降低整个类的复杂性。
答案 1 :(得分:1)
您可以从将for
- 循环的内部逻辑移动到单独的方法开始:
public void thirdRowsValidation(String thirdRowCsv) {
String[] lines1 = thirdRowCsv.split(",");
for (int i = 0; i < lines1.length; i++) {
doSomethingWithTheRow(i, lines[i]);
}
}
在doSomethingWithTheRow()
方法中,您的内部代码将驻留在:
doSomethingWithTheRow(int i, String row) {
if (indexBookngEnd == i && "".equals(temporalValidateBookngEnd)) {
temporalValidateBookngEnd = (" to " + row + "\n");
}
if (indexBookngStart == i
&& !("".equals(temporalValidateBookngEnd))) {
...
不确定这是否会将复杂程度降低到您认为可接受的水平,但这是第一次开始。此外,它是由Uncle Bob定义的清洁代码原则。你有一些小方法,做一件事(该方法现在只提取单行,然后调用其他方法来对该行做某事),哪些是短的。即,SRP
(单一责任原则)和KISS
(保持简单,愚蠢)原则。