sonarqube問題修改總結
三、可讀性問題
1、用isempty()去做容器的空或者非空判斷。
說明:use isempty() to check whether the collection is empty/not.
原因:增加可讀性。
2、刪除沒有用的import、field、class、method。
原因:增加可維護性。
修改建議:刪除無用代碼塊、申明、方法和引用。
3、if語句被拆分不利于可讀
說明:merging collapsible if statements increases the codes readability.
3、方法內多個return出口
錯誤示例:
解決建議:定義一個return,sonarqube服務器,賦值,統一返回。
4、修改了入參說明:修改了入參降低了代碼的可讀性,丟失了原始參數的值。
錯誤示例:
5、用equalsignorecase()方法替代字符串的-大小寫比較。
說明:replace these touppercase()/tolowercase()&equals() calls with a single equalsignorecase() call.
原因:參數應當是final的
解決建議,利用一個臨時變量充當需要變動的參數。
sonarqube擴展性
sonar實際上是一個web系統,展現了靜態代碼掃描的結果,sonarqube演示,結果是可以自定義的,而真正實現代碼掃描的是sonar scanner這個工具,另外同時支持多種語言的原理是它的擴展性,通過插件實現的,也就是java jar架包,可以在sonar平臺上在線安裝或者離線安裝。
sonarqube 的插件還可以對 java 以外的其他編程語言提供支持,sonarqube,對國際化以及報告文檔化也有---的支持。
sonarqube問題修改總結
四、異常處理
1、定義一個專門的異常用于異常處理
說明:using such generic excepti0ns as error, runtimeexception,throwable, andexception prevents calling methods from handling true, system-generated excepti0ns differently than application-generated errors.
錯誤示例:
修改建議:定義一個自己的異常類繼承exception用于包裝runtimeexception。
2、單個方法不要跑出多個未處理的異常