Sonar相關規則解讀2

今天繼續來看Sonar規則


3、Throwable.printStackTrace(...) should not be called

處理異常時不要用e.printStackTrace(),最好記錄log

Throwable.printStackTrace(...) prints a Throwable and its stack trace to some stream. By default that stream System.Err, which could inadvertently expose sensitive information.

Loggers should be used instead to printThrowables, as they have many advantages:

1、Users are able to easily retrieve the logs.

2、The format of log messages is uniform and allow users to browse the logs easily.

This rule raises an issue when printStackTrace is used without arguments, i.e. when the stack trace is printed to the default stream.



4、"public static" fields should be constant

There is no good reason to declare a field "public" and "static" without also declaring it "final". Most of the time this is a kludge to share a state among several objects. But with this approach, any object can do whatever it wants with the shared state, such as setting it to null.

從代碼安全性上考慮,public static 一定要加 final。


Sonar相關規則解讀1

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

推薦閱讀更多精彩內容