开发中容易引入的BUG

以自己在开发引入的BUG为例

1.对一个列表中的元素用,来分隔:

    public static String splitListByComma(List<String> dogs)
    {
        StringBuilder stringBuilder = new StringBuilder();
        for (String dogName : dogs)
        {
            stringBuilder.append(dogName).append(",");
        }
        return stringBuilder.substring(0, stringBuilder.length() - 1);
    }

BUG:当dogs为空列表时,最后一句会报越界错误(不考虑传入null的情况)。

修正后应该是

public static String splitListByComma(List<String> dogs)
    {
        if (dogs.isEmpty())
        {
            return "";
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (String dogName : dogs)
        {
            stringBuilder.append(dogName).append(",");
        }
        return stringBuilder.substring(0, stringBuilder.length() - 1);
    }
posted @ 2014-07-24 23:48  programmertata  阅读(184)  评论(0编辑  收藏  举报