最近優化代碼,記錄一下優化代碼的點

1. System.out.println 用 logger.info替代, 自己寫的main方法除外

2. e.printstacktrace() 用 logger.error替代

3. list.size>0 或者 list.size!=0 用 CollectionUtil.isNotEmpty 替代

4. List<?> list=new ArrayList<?> 用 List<?> list=new ArrayList() 替代, 自動推導

5 .StringBuffer 用 StringBuilder 替代, 真正項目中 用到 StringBuffer 的場景真的很少

6. 常量類 必須添加 private的構造方法 ?且 ?必須放在 所有定義的常量下面

7. 抽取 公共的方法 ,避免過多的 冗余代碼

8. 出現頻率高的魔法數字用常量替代

9. 用String.valueof 替換 Object+"" ? ,效率更高

10. 常量類 可以 考慮用接口 代替

11. 如果service層 很多方法都需要傳的參數, 可以考慮 ThreadLocal

12. 緩存如果在業務方面 放在 ?service層做不到 ,可以考慮 緩存 dao層?

13. 移除不需要使用的方法參數

14. Catch Exception instead of Throwable , catch 異常不要 是? Throwable

15. 數組不要加上 final修飾?

16. Return an empty collection instead of null

17. 字符串的非空判斷替換為 StringUtils.isNotBlank

18. if (code.equals("1")) { ? ? ? ? ?替換為 ? ? ? if ("1".equals(code)) { ? ? 避免空指針異常?

19. 類的構造 符合?

Class and instance variables

Constructors

Methods



20. Move the array designator from the variable to the type


21. ?變量修飾符的先后順序是 :?

The Java Language Specification recommends listing modifiers in the following order:

1. Annotations

2. public

3. protected

4. private

5. abstract

6. static

7. final

8. transient

9. volatile

10. synchronized

11. native

12. strictfp


22 .? An indexOf or lastIndexOf call with a single letter String can be made more performant by switching to a call with a char argument?

sonar 提示? :? Put single-quotes around '/' to use the faster "lastIndexOf(char)" method




23. 靜態常量命名為大寫,并以 下劃線分割?




代碼優化越來越重視了,很多公司都搭建了 sonarquebe 檢測代碼, 如果出了 報告 ,影響績效的



貼上 StringUtils 類的常用方法:?

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容