[代码优化集锦]

本随笔参考了众多资料,且在验证有效之下才或誊抄或摘录或加上自己经验组合而成。

参考资料:

1,SornaQube

2,

------------------------------------------------------------分-割-线------------------------------------------------------------

1,字符串equals比较,由确定的字符串文本主动发起,放在左侧,可防止空指针异常

        String successFlag = null;
        if("0".equals(successFlag )){
            //TODO
        }

2,嵌套的if、for、while和try语句结构不要超过3层,如果超过请考虑优化业务逻辑

if (condition1) {                  // 第1层-ok
  /* ... */
  if (condition2) {                // 第2层-ok
    /* ... */
    for(int i = 0; i < 10; i++) {  // 第3层-ok, 未超出限定
      /* ... */
      if (condition4) {            // 第4层-不ok,超出限定
        /* ... */
        return;
      }
    }
  }
}

3,涉及外部资源(比如:文件、数据库连接、网络连接等)的操作,一般会在finally中板式处理外部资源的关闭,JDK7之后可使用 try-with-resources语法简化代码

【未使用try-with-resources】
    FileInputStream inputStream = null;
    try {
        inputStream = new FileInputStream(new File("test"));
        System.out.println(inputStream.read());
    } catch (IOException e) {
        throw new RuntimeException(e.getMessage(), e);
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
                throw new RuntimeException(e.getMessage(), e);
            }
        }
    }
【使用try-with-resources】
    try (FileInputStream inputStream = new FileInputStream(new File("test"))) {
        System.out.println(inputStream.read());
    } catch (IOException e) {
        throw new RuntimeException(e.getMessage(), e);
    }

4,接上一条的情况:如果有不便于try-with-resources语法操作的关闭外部资源的时候,建议分开资源进行try-catch

        InputStream inStream = null, fileInStream = null;
        ServletOutputStream outStream = null;
        int byteRead;
        try {
            fileInStream = new FileInputStream("filename");
            inStream = new BufferedInputStream(fileInStream);
            response.reset();
            response.setContentType("APPLICATION/OCTET-STREAM");
            response.setHeader("Content-disposition", "attachment; filename=" + "filename"+ ".xls");
            outStream = response.getOutputStream();
            byte[] buffer = new byte[1024];
            while ((byteRead = inStream.read(buffer)) != -1) {
                outStream.write(buffer, 0, byteRead);
            }
            response.flushBuffer();
            outStream.close();
            inStream.close();
            fileInStream.close();
        } catch (Exception e) {
            LOGGER.error("e);
        }finally{
            try {
                if(outStream!=null){
                    outStream.close();
                }
            } catch (IOException e2) {
                LOGGER.error(e2);
            }
            try {
                if(inStream!=null){
                    inStream.close();
                }
            } catch (IOException e2) {
                LOGGER.error(e2);
            }
            try {
                if(fileInStream!=null){
                    fileInStream.close();
                }
            } catch (IOException e2) {
                LOGGER.error(e2);
            }
        }

5,

转载于:https://www.cnblogs.com/ruanian/p/11064539.html

猜你喜欢

转载自blog.csdn.net/weixin_34191845/article/details/94490668