Java 如何在使用if条件时改进代码标准?

Java 如何在使用if条件时改进代码标准?,java,if-statement,for-loop,switch-statement,Java,If Statement,For Loop,Switch Statement,假设我想在迭代列表时检查特定条件,我使用了许多违反编码标准的if条件。我有以下带有许多if条件的代码,如何在不改变输出的情况下减少编码行数并提高代码质量 在使用if条件时,请向我推荐一些好的标准 while (iterator.hasNext()) { Row nextRow = iterator.next(); for (int colIndex = 0; colIndex < 7; colIndex++) {

假设我想在迭代列表时检查特定条件,我使用了许多违反编码标准的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();
        }
while(iterator.hasNext()){
行nextRow=iterator.next();
对于(int-colIndex=0;colIndex<7;colIndex++){
如果(colIndex==0){
Cell Cell=nextRow.getCell(colIndex);
如果(单元格!=null){
firstName=cell.getStringCellValue();
System.out.println(“名字:“+firstName”);
}
}
如果(colIndex==1){
Cell Cell=nextRow.getCell(colIndex);
如果(单元格!=null){
middleName=cell.getStringCellValue();
System.out.println(“中间名:“+middleName”);
}
}
如果(colIndex==2){
Cell Cell=nextRow.getCell(colIndex);
如果(单元格!=null){
lastName=cell.getStringCellValue();
System.out.println(“姓氏:”+lastName);
}
}
如果(系数=3){
Cell Cell=nextRow.getCell(colIndex);
如果(单元格!=null){
email=cell.getStringCellValue();
System.out.println(“Email:+Email”);
}
}
如果(系数=4){
Cell Cell=nextRow.getCell(colIndex);
如果(单元格!=null){
密码=cell.getStringCellValue();
System.out.println(“密码:”+密码);
}
}
如果(系数=5){
Cell Cell=nextRow.getCell(colIndex);
如果(单元格!=null){
role=cell.getStringCellValue();
System.out.println(“角色:“+Role”);
}
}
如果(系数=6){
Cell Cell=nextRow.getCell(colIndex);
如果(单元格!=null){
status=cell.getStringCellValue();
System.out.println(“状态:+状态”);
}
}
}
System.out.println();
}

首先将
if
切换到
开关
。然后,在每种情况下,只需写一次就可以减少前两行

 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.. 
  }
 }
for(int-colIndex=0;colIndex<7;colIndex++){
Cell Cell=nextRow.getCell(colIndex);
如果(单元格!=null){
value=cell.getStringCellValue();
开关(intParam){
案例1:
System.out.println(“名字:“+firstName”);
打破
案例2:
//等等。。
}
}

注意:不要手头有IDE,如果有编译时错误,请解决。

使用
开关语句
处理
如果
,如下所示。
在最坏的情况下使用switch时,编译器将生成与
if-else
链相同的代码。其他情况下,它将提供比
if-else
更好的性能。最好先在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();
}
while(iterator.hasNext()){
行nextRow=iterator.next();
对于(int-colIndex=0;colIndex<7;colIndex++){
开关(colIndex){
案例0:
Cell Cell=nextRow.getCell(colIndex);
如果(单元格!=null){
firstName=cell.getStringCellValue();
System.out.println(“名字:“+firstName”);
}
打破
案例1:
Cell Cell=nextRow.getCell(colIndex);
如果(单元格!=null){
middleName=cell.getStringCellValue();
System.out.println(“中间名:“+middleName”);
}
打破
案例2:
Cell Cell=nextRow.getCell(colIndex);
如果(单元格!=null){
lastName=cell.getStringCellValue();
System.out.println(“姓氏:”+lastName);
}
打破
案例3:
Cell Cell=nextRow.getCell(colIndex);
如果(单元格!=null){
email=cell.getStringCellValue();
System.out.println(“Email:+Email”);
}
打破
案例4:
Cell Cell=nextRow.getCell(colIndex);
如果(单元格!=null){
密码=cell.getStringCellValue();
System.out.println(“密码:”+密码);
}
打破
案例5:
Cell Cell=nextRow.getCell(colIndex);
如果(单元格!=null){
role=cell.getStringCellValue();
System.out.println(“角色:“+Role”);
}
打破
案例6:
Cell Cell=nextRow.getCell(colIndex);
如果(单元格!=null){
status=cell.getStringCellValue();
System.out.println(“状态:+状态”);
}
打破
}
}
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 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());
            }
        }
    }
}
enum字段
...
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;
}