本文主要講述兩個方面:
1)為何制定Radio_Inspections 規(guī)范及實操
2)IDE Inspection 使用?
第一部分 :為何制定Radio_Inspections 規(guī)范及實操
一 、制定Radio_Inspections 規(guī)范的目的
? ? (1)寫代碼時發(fā)現(xiàn)并解決不規(guī)范的書寫或潛在的bug (其他工具未掃描出來)
? ? (2)解決部分代碼性能問題
二、Radio_Inspections 能夠解決的問題
? ? ? ?當前雖然IDE Inspection 能夠提供實時提示滑进,但是往往開發(fā)者沒太注意霜浴,原因主要有以下幾點:
? ? (1)IDE Inspection 默認檢查項過多揣云,掃描結(jié)果過多薄腻,處理費時,不愿處理陨收;
? ? (2)IDE Inspection 提示級別多為warning 肛搬,讓人容易忽視(有時往往會產(chǎn)生致命np問題)没佑;
? ? (3)IDE Inspection 部分檢查項,默認關(guān)閉的(規(guī)則太多温赔,開發(fā)者也不會一一瀏覽過濾)蛤奢;
? ? ? ? ? 針對上述三個問題,freddy想出相應對策陶贼,來制定符合自身項目的Inspection 規(guī)則(Radio_Inspections) :
(1)瀏覽相關(guān)規(guī)則啤贩,結(jié)合平時開發(fā),將部分默認未開啟有意義的檢查項開啟拜秧;
(2)將提示級別設(shè)置為error 痹屹;
(3)優(yōu)先處理error 級別問題。
Radio_Inspections新增 規(guī)則如下(暫時未引入阿里插件檢查項):
(1)Class structure
? ? ? ? 1) Utility class without ‘private’ constructor
? ? ? ? ? ? ?工具類 建議添加 私有構(gòu)造方法枉氮,防止實例化
? ? ? ? 2)Class with only ‘private’ constructors should be declared ‘final'
? ? ? ? ? ? ? 工具類 建議 添加final 修飾符
(2)Code style issues?
? ? ? ? 1)?‘expression.equals(“l(fā)iteral”) rather than “l(fā)iteral”.equals(expression)
? ? ? ? ? ? ? ?防止?jié)撛趎p
(3)Control flow issues
? ? ? ? 1) Unnecessary ‘null’ check before ‘instanceof’ expression ?
(4)Declaration redundancy
? ? ? ? 1) Access static member via instance reference
(5)Imports
? ? ? ? 1)Static import
(6)Java language level migration aids?
? ? ?1) Identical ‘catch’ branches in ‘try’ statement (語法糖)
? ? ?案例:
? ? 同時志衍,對應關(guān)閉 “Multiple-catch can be split into separate catch blocks”?
(7)Memory issues
? ? ? 1)Inner class may be ‘static’
(8)Probable bugs
? ? ? ? 1)Return of "null"
? ? ? ? public 方法返回 null 暖庄,建議添加 @Nullable (強制添加,不然提示)
? ? ? ?2) equals( ) and hashCode( ) not paired?
? (9) Inheritance issues
? ? ? ? ? ?1) @Overridate
?(10) Numeric issues
? ? ? ? ?1)Divide by zero
? ? ? ? ?2)?'long' literal ending with "l" instead of "L'
(11) Serialization issues?
? ? ? 1)Serializable class without 'serialVersionUID'
? ? ? ?2)‘serialVersionUID’ field not declared 'private static final long'
? ? ? ?3)Transient field in non-serializable class
? ? ? ? ? ? ?防止 transient 關(guān)鍵字 和 @Transient 用混
另外楼肪,有一些比較好的建議(暫時未添加):
(1)Performance issues?
? ? ? 1)Collection without initial capacity (638+)
? ? ? ? ? ? 設(shè)置初始容量
(2)? Magic number ?(1273 個)
建議關(guān)閉的默認規(guī)則 :
(1)Javadoc issues ?
? ? ? ? 直接checkStyle 就可以
(2)General? Default File template ? (模板)
? ? ? ? ?This inspections reports usage of the default file template for File Header.
(3)Syntax error inspection
? ? ? ? ? Allows you to see syntax errors in the results of batch code inspection.
(4)Spelling?
(5)Android Lint :Internationlization
? ? ? ? 1) Using left / right instead of start/end attributes (1640+)
第二部分? :IDE Inspection 使用?
一培廓、默認情況 ?(Project Default)
1)Specify Inspection scope?
? ? ? 選擇Inspection scope? 以及 Inspection profile?
? ? ? ?Inspections profile 主要有兩種存儲方式 ?:IDE (選擇IDE即可) ?和 project,建議使用IDE 存儲春叫,切換分支不會發(fā)生變化肩钠;
二、規(guī)則挑選過濾 ?
? ? ? ?Inspection 檢查 主要 分為 :Error 暂殖、Blocker 价匠、Critical、Major 等等? 央星”矍蓿可以通過左側(cè)漏斗按鈕進行相關(guān)過濾备韧。
三、過濾機制 ?
(1)檢查結(jié)果相關(guān)點
? ? ? ? 右側(cè)對應Suppress 方式 (分別針對 method/class/ all inspections for class)问裕,盡可能選擇粒度較小的方式廉沮,如method 颓遏。
(2)具體Method過濾?
(3)檢查項不配置,則對整個工程都不會檢查