zoukankan      html  css  js  c++  java
  • 论一段简单的代码的更改

    以下代码有何问题

        public static String decodeBuffer(String str ,String charset){
            if (str == null) {
                return str = StringUtils.EMPTY;
            }
             try {
                byte[] byteStr = new BASE64Decoder().decodeBuffer(str.trim());
                return new String(byteStr,charset);
            } catch (UnsupportedEncodingException e) {
                return new String();
            } catch (IOException e) {
                return new String();
            }
        }

    改成这样会不会好一些?(更改对空串的处理,如果为空串就直接返回空串)

        public static String decodeBuffer(String str, String charset) {
            String decodeStr = StringUtils.EMPTY;
            if (!StringUtils.isBlank(str)) {
                try {
                    byte[] byteStr = new BASE64Decoder().decodeBuffer(str);
                    return new String(byteStr, charset);
                } catch (UnsupportedEncodingException e) {
                    return new String(); // 异常处理有问题
                } catch (IOException e) {
                    return new String(); // 异常处理需要特殊处理
                }
            }
            return decodeStr;
        }

     有时候在想写程序应该写到什么地步?精益求精,还是适可而止,而作为程序员,有时候真应该需要一点执着精神,不求最好,但求更好……

    再改一改(添加对异常日志记录,保证方法只有一个return):

        public static String decodeBuffer(String str, String charset) {
            String decodeStr = StringUtils.EMPTY;
            if (!StringUtils.isBlank(str)) {
                try {
                    byte[] byteStr = new BASE64Decoder().decodeBuffer(str);
                    decodeStr = new String(byteStr, charset);
                } catch (UnsupportedEncodingException e) {
                    log.debug("UnsupportedEncodingException happened:" + e.getMessage()); // 记录异常日志
                } catch (IOException e) {
                    log.debug("IOException happened:" + e.getMessage()); // 记录异常日志
                }
            }
            return decodeStr;
        }
  • 相关阅读:
    tushare学习
    TVP-VAR模型
    时间序列分析
    python tusahre使用
    金融大讲堂-笔记
    多元GARCH模型
    方差与协方差
    代码生成器,项目更新第一版,mybatis-plus
    Springboot手动搭建项目——配置mybatis tk框架
    java基础,集合,HashMap,源码解析
  • 原文地址:https://www.cnblogs.com/garinzhang/p/3467158.html
Copyright © 2011-2022 走看看