Java代码优化案例1:IF语句优化

业务中用到了一些判断语句,最开始写在了具体业务方法里面:如下

优化前:

 /**
     * 重点设备及缺陷 数据对象解析
     *
     * @param request
     * @throws IOException
     */
    public static<T> List<T> getImportDataEquipmentDefect(String[] fields, Class<T> clz, HttpServletRequest request)
            throws IOException, InvalidFormatException, SecurityException, NoSuchMethodException, IllegalAccessException, NoSuchFieldException, InvocationTargetException, InstantiationException {
        Workbook wb = getWorkbookFromRequest(request);
        Sheet sheet = wb.getSheetAt(4);
        String departmentName="";
        String maintenanceProject="";
        String dailyPlanDate =null;
        List<T> datas = new ArrayList<>();
        // 获取总行数
        int rows = sheet.getPhysicalNumberOfRows();
        if (rows >= 2) {
            departmentName = sheet.getRow(1).getCell(1).getStringCellValue();
            maintenanceProject = sheet.getRow(3).getCell(0).getStringCellValue();
            dailyPlanDate = getCellValue((HSSFRow)sheet.getRow(1), 10);
        }
            for (int i = 3; i <= sheet.getLastRowNum(); i++) {
                Row row = sheet.getRow(i);
                if (row == null) {
                    continue;
                }
                if (fields != null) {
                    T obj = clz.getDeclaredConstructor().newInstance();
                    for (int  j= 0;  j< fields.length; j++) {
                        Cell cell = row.getCell(j);
                        String cellValue = getCellValue(cell);
                        String fieldName = fields[j];
                        Field field = null;
                        try {
                            field = obj.getClass().getDeclaredField(fieldName);
                        } catch (NoSuchFieldException e) {
                            field = obj.getClass().getSuperclass().getDeclaredField(fieldName);
                        }
                        field.setAccessible(true);
                        if(fieldName.equals("departmentName")){
                            cellValue = departmentName;
                        }
                        if(fieldName.equals("maintenanceProject")){
                            cellValue = maintenanceProject;
                        }
                        if(fieldName.equals("dailyPlanDate")){
                            cellValue = dailyPlanDate;
                        }
                        setFieldValue(field, obj, cellValue);
                    }
                    datas.add(obj);
                }
            }
        return datas;

    }

优化后:

直接调用这个setCellValue就行

public void setCellValue(String fieldName) {
    String cellValue = "";
    
    switch(fieldName) {
        case "departmentName":
            cellValue = departmentName;
            break;
        case "maintenanceProject":
            cellValue = maintenanceProject;
            break;
        case "dailyPlanDate":
            cellValue = dailyPlanDate;
            break;
        default:
            break;
    }   
   
}

优化之前代码的不好之处在于,使用多个if语句来判断字段名,这样会使代码显得冗长,并且不易于维护,当需要添加更多字段时,需要不断添加新的if语句。而优化之后的代码使用了switch语句,可以更简洁地处理这种情况,并且易于扩展,当需要添加新的字段时,只需要在switch语句中添加对应的case即可。这样可以提高代码的可读性和可维护性。

猜你喜欢

转载自blog.csdn.net/weixin_39709134/article/details/132475642