gpt4 book ai didi

java - 将方法的复杂度降低 14

转载 作者:行者123 更新时间:2023-11-30 08:00:29 25 4
gpt4 key购买 nike

Pmd 告诉我这个方法 (thirdRowsValidation) 的复杂度为 14,但我无法找到减少代码的模式。

indexBookngEnd、indexTravelStart ...所有这些变量都是在另一个循环迭代中创建的其他数组的索引(csv 文件列的标题)- Ref1

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”的圈复杂度为 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 |

最佳答案

该方法的复杂性很高,因为它要做很多不同的事情。尝试每种方法做一件事。

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

尝试将方法所需的所有内容作为其参数之一传递。方法不应依赖于在不同方法中设置的字段的值。这样可以降低复杂性,并使代码更易于阅读、理解和测试。

如果一个方法或类具有很高的复杂性,那么通常意味着它试图同时做太多不同的事情。退后一步,尝试确定它所做的不同事情并分别实现它们。这将自动生成复杂度较低的代码。

将内部循环提取到新方法中通常是一种快速破解方法,有助于降低单个方法的复杂性,但不会降低整个类的复杂性。

关于java - 将方法的复杂度降低 14,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38495626/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com