soanr修改记录

439 阅读1分钟

记录一些经常遇到或典型的问题,有些是旧代码问题,有些是自己没意识到的问题。

bug

  • NullPointerException

    好多是因为使用了工具类判断,sonar没扫描出来误判了,这情况可以新建规则

    参考官网,未验证

  • 随机数

public void doSomethingCommon() {
  Random rand = new Random();  // Noncompliant; new instance created with each invocation
  int rValue = rand.nextInt();
  //...
private Random rand = SecureRandom.getInstanceStrong();  // SecureRandom is preferred to Random

public void doSomethingCommon() {
  int rValue = this.rand.nextInt();
  //...

漏洞

安全

SSLContext.getInstance("TLSv1.2")

TLS --> TLSv1.2

log

e.printStackTrace()

System.out.print()

改成 --> Slf4j写文件日志形式

异味

非解析性注释,比如注释了不要的代码,应该删除,如果要找回之前的代码可以通过Git记录。

重复

抽离重复代码行

覆盖率

单元测试