간단한 코드의 변경을 논하다

4794 단어 코드
다음 코드에 무슨 문제가 있습니까
    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;

    }

좋은 웹페이지 즐겨찾기