最近在發(fā)布驗證過程中發(fā)現(xiàn)的2個代碼問題:
問題代碼1:
1.如下:當context.getExtendMap()不含F(xiàn)ORM_CODE_KEY時,期望是sellerCoordinationFormCode為null,這樣后續(xù)的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和功能測試二者交叉保證問題不被漏測,不要在不了解代碼細節(jié)的情況下預設期望(比如預設String.valueOf(null)會返回null)。
問題代碼2:
定義了接口的入?yún)ο?,對象中有個字段timeout類型為int(后續(xù)修改為Integer)

下面這段代碼原本期望當timeout不傳入任何值,設置timeout為3000,然后實際當timeout不傳入任何值時,timeout默認值不是null而是0

實測int類型會被初始賦值0。
