代码与流程规范
2019-05-22 17:36
295 查看
本文整理在开发过程中遇到的一些代码规范问题,主要依据是SonarLint的代码质量检测。
1、工具类不应该存在公有构造方法
Utility classes, which are collections of static members, are not meant to be instantiated. Even abstract utility classes, which can be extended, should not have public constructors.
Java adds an implicit public constructor to every class which does not define at least one explicitly. Hence, at least one non-public constructor should be defined.
不好的例子
class StringUtils { public static String concatenate(String s1, String s2) { return s1 + s2; } }
好的例子
class StringUtils { private StringUtils() { throw new IllegalStateException("Utility class"); } public static String concatenate(String s1, String s2) { return s1 + s2; } }
Assignments should not be made from within sub-expressions Assignments within sub-expressions are hard to spot and therefore make the code less readable. Ideally, sub-expressions should not have side-effects. Noncompliant Code Example if ((str = cont.substring(pos1, pos2)).isEmpty()) { // Noncompliant //... Compliant Solution str = cont.substring(pos1, pos2); if (str.isEmpty()) { //... Exceptions Assignments in while statement conditions, and assignments enclosed in relational expressions are ignored. BufferedReader br = new BufferedReader(/* ... */); String line; while ((line = br.readLine()) != null) {...} Chained assignments, including compound assignments, are ignored. int i = j = 0; int k = (j += 1); result = (bresult = new byte[len]);
相关文章推荐
- 通过Git分支来规范代码上线流程
- 项目管理---git----快速使用git笔记(七)------coding.net项目管理多人操作的流程规范--合并代码审核
- C#基础知识复习1代码规范-执行流程(c#)-面向对象-引用命名空间-封装-继承-访问修饰符-虚方法-静态成员-多态-抽象类等
- 基于git的代码版本管理规范及流程-简版
- 总结PHP代码规范、流程规范、git规范
- 项目管理---git----快速使用git笔记(七)------coding.net项目管理多人操作的流程规范--合并代码审核
- 思考:重复性的代码流程应该导出为模板也方便规范
- 前端文档汇总(含代码规范、开发流程、知识分享,持续更新)
- 一看就懂系列之 构建一套简单但规范的php代码上线流程
- 软件流程规范---java代码规范
- 良好的书写规范提高PHP代码执行效率【上】
- 前端代码规范整理
- iOS 代码注释规范
- RSS介绍、RSS 2.0规范说明和示例代码
- 详解 ESLint 规则,规范你的代码
- 一般代码规范,General Regulations for Coding (quanben,2007-3-21试行)
- 完整的REM布局的工作流程与规范
- 以流程设计为向导的现实代码(引以为戒!)
- google的html、css代码规范(转)
- Python 代码规范检查