簡單舉幾個CodeReview的常見錯誤

2021-10-03 19:28:51 字數 2521 閱讀 6397

簡單舉幾個codereview的常見錯誤

今天給公司某個專案做個codereview,用findbugs預設配置規則跑了下,發現了幾個問題,都是平時coding時稍微注意下,就能避免這樣的不適。

1、空指標引用

load of known null value

priority:

medium confidence dodgy code

problem classification:

dodgy code (null pointer dereference)

np_load_of_known_null_value (load of known null value)

notes:

value loaded from partyid

loadofknownnullvalue (np)

if

(null==

partyid&&

partyid==

0)

又或如:

if

(null==

partyids&&

partyids

.size()

==0)

2、程式沒有及時return,導致後續流程空指標異常

partytrailerlist

partytrailerlistcheck

=partytrailerlistservice

.selectbyprimarykey

(partytrailerlist

.getpartytrailerlistid()

);if(

partytrailerlistcheck==

null)if

(stringutils

.equals

("審核中"

,partytrailerlistcheck

.getauthstatus()

)||stringutils

.equals

("已審核"

,partytrailerlistcheck

.getauthstatus()

))

3、對乙個已知是null的物件作非空判斷

redundant nullcheck of list which is known to be null;

list

<

map<

string

,object

>

>

list

=null

;list

<

partyaccount

>

partyaccountlist

=null

;try

catch

(exceptione)

if(null!=

list).

..return

list

;

這裡就是個多餘的操作。list至始至終沒有被例項化,必null.

if(null != list)

4、不用對乙個已知非空物件作空指標判斷

redundant nullcheck of listpartyid, which is known to be non-null

list listpartyid = new arraylist<

>()

; list> resultlist = new arraylist<

>()

; //根據businesslicense查詢organization列表

list list = organizationservice.selectorganizationlistbybusinesslicense(businesslicense)

; if(list != null && list.size(

)> 0)

} //2.根據根據businesslicense查詢person列表

list personlist = personservice.selectpersonlistbybusinesslicense(businesslicense)

; if(personlist != null && personlist.size(

)> 0)}if

(listpartyid != null || listpartyid.size(

)> 0)

listpartyid必然不為空,多餘判斷。

5、判斷條件有誤

if

(partytrailerlist!=

null

)

partytrailerlist此處非空是准入條件,內部的**邏輯再已partytrailerlist是否為空做條件,肯定就錯了。

Code Review中的幾個提示

原文 http coolshell.cn p 1302酷殼 code review中的幾個提示 陳皓 code review應該是軟體工程最最有價值的乙個活動,之前,本站發表過 簡單實用的code review工具 首先,我們先來看看code reivew的用處 code reviews 中,可以通...

Code Review中的幾個提示

原文 酷殼 code review中的幾個提示 陳皓 code review應該是軟體工程最最有價值的乙個活動,之前,本站發表過 簡單實用的code review工具 首先,我們先來看看code reivew的用處 code reviews 中,可以通過大家的建議增進 的質量。code review...

Code Review中的幾個提示

code review應該是軟體工程最最有價值的乙個活動,之前,本站發表過 簡單實用的code review工具 首先,我們先來看看code reivew的用處 code reviews 中,可以通過大家的建議增進 的質量。code reviews 是乙個傳遞知識的手段,可以讓其它並不熟悉 的人知道...