记录遇到的2个代码问题

最近在发布验证过程中发现的2个代码问题:

问题代码1:

1.如下:当context.getExtendMap()不含FORM_CODE_KEY时,期望是sellerCoordinationFormCode为null,这样后续的if condition会判定不通过


2.但实际是if判断通过,这是因为:String.valueOf(null) 返回"null"而不是null,见源码:

    /**
     * Returns the string representation of the {@code Object} argument.
     *
     * @param   obj   an {@code Object}.
     * @return  if the argument is {@code null}, then a string equal to
     *          {@code "null"}; otherwise, the value of
     *          {@code obj.toString()} is returned.
     * @see     java.lang.Object#toString()
     */
    public static String valueOf(Object obj) {
        return (obj == null) ? "null" : obj.toString();
    }

总结下:对任何代码变更要做功能测试覆盖,哪怕是小小的一行改动,code review和功能测试二者交叉保证问题不被漏测,不要在不了解代码细节的情况下预设期望(比如预设String.valueOf(null)会返回null)。

问题代码2:

定义了接口的入参对象,对象中有个字段timeout类型为int(后续修改为Integer)



下面这段代码原本期望当timeout不传入任何值,设置timeout为3000,然后实际当timeout不传入任何值时,timeout默认值不是null而是0


实测int类型会被初始赋值0。


你可能感兴趣的:(记录遇到的2个代码问题)