使用if条件如何提高代码标准?
How to improve the code standards while using if conditions?
假设我想在迭代列表时检查特定条件,我使用了很多违反编码标准的 if 条件。我有以下带有许多 if 条件的代码,如何在不改变输出的情况下减少编码行数并提高代码质量。
请在使用 if 条件时建议我一些好的标准。
while (iterator.hasNext()) {
Row nextRow = iterator.next();
for (int colIndex = 0; colIndex < 7; colIndex++) {
if (colIndex == 0) {
Cell cell = nextRow.getCell(colIndex);
if (cell != null) {
firstName = cell.getStringCellValue();
System.out.println("First Name : "+firstName);
}
}
if (colIndex == 1) {
Cell cell = nextRow.getCell(colIndex);
if (cell != null) {
middleName = cell.getStringCellValue();
System.out.println("Middle Name : "+middleName);
}
}
if (colIndex == 2) {
Cell cell = nextRow.getCell(colIndex);
if (cell != null) {
lastName = cell.getStringCellValue();
System.out.println("Last Name : "+lastName);
}
}
if (colIndex == 3) {
Cell cell = nextRow.getCell(colIndex);
if (cell != null) {
email = cell.getStringCellValue();
System.out.println("Email : "+email);
}
}
if (colIndex == 4) {
Cell cell = nextRow.getCell(colIndex);
if (cell != null) {
password = cell.getStringCellValue();
System.out.println("Password : "+password);
}
}
if (colIndex == 5) {
Cell cell = nextRow.getCell(colIndex);
if (cell != null) {
role = cell.getStringCellValue();
System.out.println("Role : "+role);
}
}
if (colIndex == 6) {
Cell cell = nextRow.getCell(colIndex);
if (cell != null) {
status = cell.getStringCellValue();
System.out.println("Status : "+status);
}
}
}
System.out.println();
}
首先将if
移到switch
。然后你可以减少每个条件下的前两行只写一次
for (int colIndex = 0; colIndex < 7; colIndex++) {
Cell cell = nextRow.getCell(colIndex);
if (cell != null) {
value= cell.getStringCellValue();
switch (intParam) {
case 1:
System.out.println("First Name : "+firstName);
break;
case 2 :
// and so on..
}
}
注意:手头没有IDE,如有编译时错误请解决。
使用 switch statement
作为 if
,如下所示。
当您在最坏的情况下使用 switch 时,编译器将生成与 if-else
链相同的代码。其他时候它会提供比 if-else
更好的性能。最好将最常见的情况 1st 用于 switch 语句。
while (iterator.hasNext()) {
Row nextRow = iterator.next();
for (int colIndex = 0; colIndex < 7; colIndex++) {
switch(colIndex){
case 0 :
Cell cell = nextRow.getCell(colIndex);
if (cell != null) {
firstName = cell.getStringCellValue();
System.out.println("First Name : "+firstName);
}
break;
case 1 :
Cell cell = nextRow.getCell(colIndex);
if (cell != null) {
middleName = cell.getStringCellValue();
System.out.println("Middle Name : "+middleName);
}
break;
case 2 :
Cell cell = nextRow.getCell(colIndex);
if (cell != null) {
lastName = cell.getStringCellValue();
System.out.println("Last Name : "+lastName);
}
break;
case 3 :
Cell cell = nextRow.getCell(colIndex);
if (cell != null) {
email = cell.getStringCellValue();
System.out.println("Email : "+email);
}
break;
case 4 :
Cell cell = nextRow.getCell(colIndex);
if (cell != null) {
password = cell.getStringCellValue();
System.out.println("Password : "+password);
}
break;
case 5 :
Cell cell = nextRow.getCell(colIndex);
if (cell != null) {
role = cell.getStringCellValue();
System.out.println("Role : "+role);
}
break;
case 6 :
Cell cell = nextRow.getCell(colIndex);
if (cell != null) {
status = cell.getStringCellValue();
System.out.println("Status : "+status);
}
break;
}
}
System.out.println();
}
大部分代码在每个分支中保持不变:
Cell cell = nextRow.getCell(colIndex);
String value = null;
if (cell != null) {
value = cell.getStringCellValue(); // the name of the variable differs here, but it's always a string?
...因此您可以在 if 块之前执行此操作。如果它不总是一个字符串,您至少可以在 if 之前获取单元格。然后你可以使用开关盒...
switch(colIndex) {
case 1: firstName = value; break;
//etc.
如果分支很复杂,您可能会考虑使用枚举。
enum CellValue {
FIRST_NAME(0) {
public void doSomething(Cell cell) {
...do something.
}
},
// etc.
private final int colIndex;
private CellValue(int colIndex) {
this.colIndex = colIndex;
}
public abstract void doSomething(Cell cell);
public static void work(int colIndex, Something nextRow) {
for(CellValue value : values()) {
if (value.colIndex == colIndex) {
value.work(newRow.getCell(colIndex));
return;
}
}
// throw exception here
}
}
这样,您就可以简单地调用...
CellValue.work( colIndex, nextRow );
你有一个绝好的机会 enum
:
enum Field {
FirstName,
MiddleName,
LastName,
EMail,
Password,
Role,
Status;
}
public void test() {
while (iterator.hasNext()) {
Row nextRow = iterator.next();
Map<Field, String> values = new EnumMap(Field.class);
for (Field f : Field.values()) {
Cell cell = nextRow.getCell(f.ordinal());
if (cell != null) {
values.put(f, cell.getStringCellValue());
}
}
}
}
取决于您的列的固定程度。由于您目前使用 for 循环并没有从中获益太多,因此您甚至可以直接存储数据。
...
while (iterator.hasNext()) {
Row next = iterator.next();
firstName = saveGetCell(firstName, 0, next);
middleName = saveGetCell(middleName, 1, next);
lastName = saveGetCell(lastName, 2, next);
email = saveGetCell(email, 3, next);
password = saveGetCell(password, 4, next);
role = saveGetCell(role, 5, next);
status = saveGetCell(status, 6, next);
}
private String saveGetCell(String saveValue, int index, Row row) {
Cell cell = row.getCell(index);
String result = null;
if(cell != null) {
result = cell.getStringCellValue();
}
return result == null ? saveValue : result;
}
private void CycleFunc()
{
while (iterator.hasNext()) {
Row nextRow = iterator.next();
for (int colIndex = 0; colIndex < 7; colIndex++)
{
String result = GetCellStatus(colIndex);
System.out.println(result);
}
System.out.println();
}
}
private string GetCellStatus(int colIndex)
{
String result;
switch(colIndex)
{
case:0:
result = "Middle Name : "+GetStingFromCell(colIndex);
break;
case:1:
result = "Last Name : "+GetStingFromCell(colIndex);
break;
case:2:
result = "Email : "+GetStingFromCell(colIndex);
break;
case:3:
result = "Password: "+GetStingFromCell(colIndex);
break;
case: 4:
result = "Role : "+GetStingFromCell(colIndex);
break;
case:5:
result = "Status : "+GetStingFromCell(colIndex);
break;
case:6:
result = "Middle Name : "+GetStingFromCell(colIndex);
break;
default:
result = "Some error";
break;
}
return result;
}
private string GetStingFromCell(int colIndex)
{
String str;
Cell cell = nextRow.getCell(colIndex);
if (cell != null) {
str = cell.getStringCellValue();
}
return str;
}
假设我想在迭代列表时检查特定条件,我使用了很多违反编码标准的 if 条件。我有以下带有许多 if 条件的代码,如何在不改变输出的情况下减少编码行数并提高代码质量。
请在使用 if 条件时建议我一些好的标准。
while (iterator.hasNext()) {
Row nextRow = iterator.next();
for (int colIndex = 0; colIndex < 7; colIndex++) {
if (colIndex == 0) {
Cell cell = nextRow.getCell(colIndex);
if (cell != null) {
firstName = cell.getStringCellValue();
System.out.println("First Name : "+firstName);
}
}
if (colIndex == 1) {
Cell cell = nextRow.getCell(colIndex);
if (cell != null) {
middleName = cell.getStringCellValue();
System.out.println("Middle Name : "+middleName);
}
}
if (colIndex == 2) {
Cell cell = nextRow.getCell(colIndex);
if (cell != null) {
lastName = cell.getStringCellValue();
System.out.println("Last Name : "+lastName);
}
}
if (colIndex == 3) {
Cell cell = nextRow.getCell(colIndex);
if (cell != null) {
email = cell.getStringCellValue();
System.out.println("Email : "+email);
}
}
if (colIndex == 4) {
Cell cell = nextRow.getCell(colIndex);
if (cell != null) {
password = cell.getStringCellValue();
System.out.println("Password : "+password);
}
}
if (colIndex == 5) {
Cell cell = nextRow.getCell(colIndex);
if (cell != null) {
role = cell.getStringCellValue();
System.out.println("Role : "+role);
}
}
if (colIndex == 6) {
Cell cell = nextRow.getCell(colIndex);
if (cell != null) {
status = cell.getStringCellValue();
System.out.println("Status : "+status);
}
}
}
System.out.println();
}
首先将if
移到switch
。然后你可以减少每个条件下的前两行只写一次
for (int colIndex = 0; colIndex < 7; colIndex++) {
Cell cell = nextRow.getCell(colIndex);
if (cell != null) {
value= cell.getStringCellValue();
switch (intParam) {
case 1:
System.out.println("First Name : "+firstName);
break;
case 2 :
// and so on..
}
}
注意:手头没有IDE,如有编译时错误请解决。
使用 switch statement
作为 if
,如下所示。
当您在最坏的情况下使用 switch 时,编译器将生成与 if-else
链相同的代码。其他时候它会提供比 if-else
更好的性能。最好将最常见的情况 1st 用于 switch 语句。
while (iterator.hasNext()) {
Row nextRow = iterator.next();
for (int colIndex = 0; colIndex < 7; colIndex++) {
switch(colIndex){
case 0 :
Cell cell = nextRow.getCell(colIndex);
if (cell != null) {
firstName = cell.getStringCellValue();
System.out.println("First Name : "+firstName);
}
break;
case 1 :
Cell cell = nextRow.getCell(colIndex);
if (cell != null) {
middleName = cell.getStringCellValue();
System.out.println("Middle Name : "+middleName);
}
break;
case 2 :
Cell cell = nextRow.getCell(colIndex);
if (cell != null) {
lastName = cell.getStringCellValue();
System.out.println("Last Name : "+lastName);
}
break;
case 3 :
Cell cell = nextRow.getCell(colIndex);
if (cell != null) {
email = cell.getStringCellValue();
System.out.println("Email : "+email);
}
break;
case 4 :
Cell cell = nextRow.getCell(colIndex);
if (cell != null) {
password = cell.getStringCellValue();
System.out.println("Password : "+password);
}
break;
case 5 :
Cell cell = nextRow.getCell(colIndex);
if (cell != null) {
role = cell.getStringCellValue();
System.out.println("Role : "+role);
}
break;
case 6 :
Cell cell = nextRow.getCell(colIndex);
if (cell != null) {
status = cell.getStringCellValue();
System.out.println("Status : "+status);
}
break;
}
}
System.out.println();
}
大部分代码在每个分支中保持不变:
Cell cell = nextRow.getCell(colIndex);
String value = null;
if (cell != null) {
value = cell.getStringCellValue(); // the name of the variable differs here, but it's always a string?
...因此您可以在 if 块之前执行此操作。如果它不总是一个字符串,您至少可以在 if 之前获取单元格。然后你可以使用开关盒...
switch(colIndex) {
case 1: firstName = value; break;
//etc.
如果分支很复杂,您可能会考虑使用枚举。
enum CellValue {
FIRST_NAME(0) {
public void doSomething(Cell cell) {
...do something.
}
},
// etc.
private final int colIndex;
private CellValue(int colIndex) {
this.colIndex = colIndex;
}
public abstract void doSomething(Cell cell);
public static void work(int colIndex, Something nextRow) {
for(CellValue value : values()) {
if (value.colIndex == colIndex) {
value.work(newRow.getCell(colIndex));
return;
}
}
// throw exception here
}
}
这样,您就可以简单地调用...
CellValue.work( colIndex, nextRow );
你有一个绝好的机会 enum
:
enum Field {
FirstName,
MiddleName,
LastName,
EMail,
Password,
Role,
Status;
}
public void test() {
while (iterator.hasNext()) {
Row nextRow = iterator.next();
Map<Field, String> values = new EnumMap(Field.class);
for (Field f : Field.values()) {
Cell cell = nextRow.getCell(f.ordinal());
if (cell != null) {
values.put(f, cell.getStringCellValue());
}
}
}
}
取决于您的列的固定程度。由于您目前使用 for 循环并没有从中获益太多,因此您甚至可以直接存储数据。
...
while (iterator.hasNext()) {
Row next = iterator.next();
firstName = saveGetCell(firstName, 0, next);
middleName = saveGetCell(middleName, 1, next);
lastName = saveGetCell(lastName, 2, next);
email = saveGetCell(email, 3, next);
password = saveGetCell(password, 4, next);
role = saveGetCell(role, 5, next);
status = saveGetCell(status, 6, next);
}
private String saveGetCell(String saveValue, int index, Row row) {
Cell cell = row.getCell(index);
String result = null;
if(cell != null) {
result = cell.getStringCellValue();
}
return result == null ? saveValue : result;
}
private void CycleFunc()
{
while (iterator.hasNext()) {
Row nextRow = iterator.next();
for (int colIndex = 0; colIndex < 7; colIndex++)
{
String result = GetCellStatus(colIndex);
System.out.println(result);
}
System.out.println();
}
}
private string GetCellStatus(int colIndex)
{
String result;
switch(colIndex)
{
case:0:
result = "Middle Name : "+GetStingFromCell(colIndex);
break;
case:1:
result = "Last Name : "+GetStingFromCell(colIndex);
break;
case:2:
result = "Email : "+GetStingFromCell(colIndex);
break;
case:3:
result = "Password: "+GetStingFromCell(colIndex);
break;
case: 4:
result = "Role : "+GetStingFromCell(colIndex);
break;
case:5:
result = "Status : "+GetStingFromCell(colIndex);
break;
case:6:
result = "Middle Name : "+GetStingFromCell(colIndex);
break;
default:
result = "Some error";
break;
}
return result;
}
private string GetStingFromCell(int colIndex)
{
String str;
Cell cell = nextRow.getCell(colIndex);
if (cell != null) {
str = cell.getStringCellValue();
}
return str;
}