zoukankan      html  css  js  c++  java
  • [代码优化集锦]

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

    参考资料:

    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,

  • 相关阅读:
    wpf数据验证实例及常用方法小结
    wpf自定义colorpicker
    DataGrid绑定Dictionary问题
    DataTemplate和ControlTemplate的关系
    Validation Rule和Binding Group
    WPF converter(包含传递复杂参数)
    【链接】Eclipse的Debug调试技巧
    安装apache服务出错,无法启动此程序,因为计算机中丢失VCRUNTIME140.dll
    apache解压版安装服务
    apache——(OS 10048)通常每个套接字地址(协议/网络地址/端口)只允许使用一次。 : AH00072: make_sock: could not bind to address [::]:443
  • 原文地址:https://www.cnblogs.com/ruanian/p/11064539.html
Copyright © 2011-2022 走看看