sonarqube可以集成findbug,pwd,lint等靜態(tài)代碼檢查工具,提供統(tǒng)一的檢查平臺.
sonar代碼檢測的標(biāo)準(zhǔn):
1径密、Reliability可靠性:低于A
可靠性比率的計(jì)算方法:
A = 0 Bug 最高等級A泼差,表示代碼無bug
B = at least 1 Minor Bug 代碼只要有一個(gè)次要bug衅金,等級就為B
C = at least 1 Major Bug 只要包含一個(gè)重要bug琐旁,等級將為C
D = at least 1 Critical Bug 只要有一個(gè)嚴(yán)重bug击奶,等級評估為D
E = at least 1 Blocker Bug 只要有一個(gè)最高等級的阻斷級別的bug漾唉,可靠性評估為E炬称,最低級別伞广。
2拣帽、Security安全性:低于A
安全度指標(biāo)計(jì)算方法:
A = 0 Vulnerability 沒有漏洞時(shí),項(xiàng)目評估為最高級別A
B = at least 1 Minor Vulnerability 只要包含一個(gè)次要漏洞赔癌,項(xiàng)目評估為級別B
C = at least 1 Major Vulnerability 只要包含一個(gè)重要漏洞诞外,項(xiàng)目評估為級別C
D = at least 1 Critical Vulnerability 只要包含一個(gè)嚴(yán)重漏洞,評估為D
E = at least 1 Blocker Vulnerability 只要包含一個(gè)阻斷漏洞灾票,項(xiàng)目評估為最低級別E
3峡谊、Maintainability可維護(hù)性:低于A
可維護(hù)性等級范圍從A(非常好)到E(非常差)。 評級由技術(shù)債務(wù)比率的值決定刊苍,技術(shù)債務(wù)比率是將項(xiàng)目的技術(shù)債務(wù)與從零開始重寫代碼所需的成本進(jìn)行比較既们。 A到D的默認(rèn)值為0.05,0.1,0.2,0.5。任何超過0.5評級就為E正什。
4啥纸、Coverage覆蓋率:新增覆蓋率低于60%
Coverage
行覆蓋和條件覆蓋的混合。單元測試覆蓋多少源代碼婴氮。Coverage = (CT + CF + LC)/(2*B + EL)
其中 :
CT = conditions that have been evaluated to ‘true’ at least once至少有一次被判斷為true的條件數(shù)
CF = conditions that have been evaluated to ‘false’ at least once 至少一次被判斷為false的條件數(shù)
LC = covered lines = lines_to_cover uncovered_lines 已覆蓋的行數(shù)
B = total number of conditions 條件總數(shù)
EL = total number of executable lines (lines_to_cover) 所有可執(zhí)行的代碼總行數(shù)
5斯棒、Duplicated Lines:新增代碼重復(fù)行高于3%
其中 :重復(fù)率=重復(fù)行數(shù)/總行數(shù)*100
6盾致、Complexity復(fù)雜度:高于10
以下關(guān)鍵詞增加復(fù)雜性:if, for, while, case, catch, throw, return (不是方法的最后一個(gè)語句), &&, ||, ?
else, default,? finally不增加復(fù)雜度
代碼復(fù)雜度過高將難以理解、難以維護(hù)荣暮。
計(jì)算公式1:V(G)=E-N+2P庭惜。其中,E表示控制流圖中邊的數(shù)量穗酥,N表示控制流圖中節(jié)點(diǎn)的數(shù)量护赊,P圖的連接組件數(shù)目(圖的組件數(shù)是相連節(jié)點(diǎn)的最大集合)。因?yàn)榭刂屏鲌D都是連通的砾跃,所以P為1.
圈復(fù)雜度 代碼狀況 ?? 可測性 維護(hù)成本
1-10 ? ? 清晰骏啰、結(jié)構(gòu)化? 高 ? ? ? 低
11-20 ? 復(fù)雜 ? ? ? ? ? ? ?? 中 ? ? ?? 中
21-30? 非常復(fù)雜 ? ? ? ? 低 ? ? ?? 高
>30 ? ? 不可讀 ? ? ?? 不可測 ? ? 非常高
常遇到的問題:
壞味道,漏洞抽高,bug判耕,代碼規(guī)范。
Bugs:
1.A "NullPointerException" could be thrown; "issue" is nullable here.
空指針異常.
2.Cast one of the operands of this addition operation to a "float".
不同類型的值做運(yùn)算,需要統(tǒng)一成一種類型.
3.Use an "instanceof" comparison instead.
例如:RuntimeException.class.getName().equals(className);改成instanceof.
4.Use "BigDecimal.valueOf" instead.
new BigDecimal(teraBytes)改成valueOf.
Vulnerability:
1.Make tag a static final constant or non-public and provide accessors if needed.
public String tag;把tag變量變成靜態(tài)常量或非公共常量翘骂,并在需要時(shí)提供訪問器祈秕。
2.Use a logger to log this exception.
e.printStackTrace();改成logger.
3.Do something with the "boolean" value returned by "createNewFile".
public static File compressImage(){}返回值類型不對,改成boolean.
Code Smell:
1.Remove this unused "TAG" private field.
移除無用的自定義變量.
2.Rename this package name to match the regular expression '^[a-z]+(\.[a-z][a-z0-9]*)*$'.
包名書寫要符合正則規(guī)范.
3.Add a nested comment explaining why this method is empty, throw an UnsupportedOperationException or complete the implementation.
無用的空方法,沒有添加注釋,說明用處.
4.Introduce a new variable instead of reusing the parameter "percent".
變量名重復(fù)使用.
5.Catch Exception instead of Throwable.
捕獲異常而非直接拋出.
6.Add a private constructor to hide the implicit public one.
缺少私有構(gòu)造函數(shù).
7.This block of commented-out lines of code should be removed.
注釋掉的代碼塊應(yīng)該刪除.
8.Change this "try" to a try-with-resources.
傳統(tǒng)的try-catch-finally,當(dāng)需要關(guān)閉的資源很多時(shí),代碼finally會很多,導(dǎo)致代碼冗余.換成try-with的模式會使得代碼簡潔.