开发中容易引入的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);

    }

你可能感兴趣的:(bug)