代码质量管理: Sonarlint插件+ Sonarqube可视化页面
在提交代码前,为提升代码质量还需要使用一些静态代码工具检查代码质量,最为常用的是Sonar;在本地IDE中可以配置Sonarlint插件进行检查,我们也可以配置sonar扫描任务,通过Sonarqube可视化页面查看代码存在的问题
文章目录
- 代码质量管理: Sonarlint插件+ Sonarqube可视化页面
-
- 1、Sonar是什么?
- 2、SonarQube安装
- 3、SonarQube代码质量检测接入
- 4、sonar 常见问题及修复思路
-
- 4.1、sonar 常见问题及修复思路
-
- 1、空指针
- 2、Optional...orElseThrow()
- 3、Add a nested comment explaining why this method is empty, throw an UnsupportedOperationException or complete the implementation.
- 4、Add some tests to this class
- 5、Add at least one assertion to this test case
- 6、Rename this constant name to match the regular expression ^ [A-Z0-9]*(_[A-Z0-9]+)*$'.
- 7、Remove usage of generic wildcard type
- 8、在抛错InterruptedException()异常后,需要使用Thread.interrupt()重新中断线程,否则可能导致丢失线程被中断的信息
- 9、流在使用后需要及时关闭
- 10、Use a thread-safe type; adding "volatile" is not enough to make this field thread-safe.
- 11、Call "Optional#isPresent()" or "!Optional#isEmpty()" before accessing the value.
- 4.2、抑制/忽略告警
- 4.3、代码质量指标统计
- 参考文档
1、Sonar是什么?
Sonar是一个用于代码质量管理的开源平台,用于管理源代码的质量。Sonar 可以集成不同的测试工具,代码分析工具,以及持续集成工具。与持续集成工具(例如 Hudson/Jenkins 等)不同,Sonar 并不是简单地把不同的代码检查工具结果(例如 FindBugs,PMD 等)直接显示在 Web 页面上,而是通过不同的插件对这些结果进行再加工处理,通过量化的方式度量代码质量的变化,从而可以方便地对不同规模和种类的工程进行代码质量管理。
Sonar可以从以下七个维度检测代码质量,而作为开发人员至少需要处理前5种代码质量问题
- 不遵循代码标准 sonar可以通过PMD,CheckStyle,Findbugs等等代码规则检测工具规范代码编写;
- 潜在的缺陷 son