16 条 yyds 的代码规范 下
2023-03-14 22:52:04 时间
十一、字符串转化使用String.valueOf(value) 代替 " " + value
把其它对象或类型转化为字符串时,使用String.valueOf(value) 比 ""+value 的效率更高。
反例:
//把其它对象或类型转化为字符串反例: int num = 520; // "" + value String strLove = "" + num;
正例:
//把其它对象或类型转化为字符串正例: int num = 520; // String.valueOf() 效率更高 String strLove = String.valueOf(num);
十二、避免使用BigDecimal(double)
BigDecimal(double) 存在精度损失风险,在精确计算或值比较的场景中可能会导致业务逻辑异常。
反例:
// BigDecimal 反例 BigDecimal bigDecimal = new BigDecimal(0.11D);
正例:
// BigDecimal 正例 BigDecimal bigDecimal1 = bigDecimal.valueOf(0.11D);
图1. 失去精度
十三、返回空数组和集合而非 null
若程序运行返回null,需要调用方强制检测null,否则就会抛出空指针异常;返回空数组或空集合,有效地避免了调用方因为未检测null 而抛出空指针异常的情况,还可以删除调用方检测null 的语句使代码更简洁。
反例:
//返回null 反例 public static Result[] getResults() { return null; } public static List<Result> getResultList() { return null; } public static Map<String, Result> getResultMap() { return null; }
正例:
//返回空数组和空集正例 public static Result[] getResults() { return new Result[0]; } public static List<Result> getResultList() { return Collections.emptyList(); } public static Map<String, Result> getResultMap() { return Collections.emptyMap(); }
十四、优先使用常量或确定值调用equals 方法
对象的equals 方法容易抛空指针异常,应使用常量或确定有值的对象来调用equals 方法。
反例:
//调用 equals 方法反例 private static boolean fileReader(String fileName)throws IOException{ // 可能抛空指针异常 return fileName.equals("Charming"); }
正例:
//调用 equals 方法正例 private static boolean fileReader(String fileName)throws IOException{ // 使用常量或确定有值的对象来调用 equals 方法 return "Charming".equals(fileName); //或使用:java.util.Objects.equals() 方法 return Objects.equals("Charming",fileName); }
十五、枚举的属性字段必须是私有且不可变
枚举通常被当做常量使用,如果枚举中存在公共属性字段或设置字段方法,那么这些枚举常量的属性很容易被修改;理想情况下,枚举中的属性字段是私有的,并在私有构造函数中赋值,没有对应的Setter 方法,最好加上final 修饰符。
反例:
public enum SwitchStatus { // 枚举的属性字段反例 DISABLED(0, "禁用"), ENABLED(1, "启用"); public int value; private String description; private SwitchStatus(int value, String description) { this.value = value; this.description = description; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } }
正例:
public enum SwitchStatus { // 枚举的属性字段正例 DISABLED(0, "禁用"), ENABLED(1, "启用"); // final 修饰 private final int value; private final String description; private SwitchStatus(int value, String description) { this.value = value; this.description = description; } // 没有Setter 方法 public int getValue() { return value; } public String getDescription() { return description; } }
十六、tring.split(String regex)部分关键字需要转译
使用字符串String 的plit 方法时,传入的分隔字符串是正则表达式,则部分关键字(比如 .[]()| 等)需要转义。
反例:
// String.split(String regex) 反例 String[] split = "a.ab.abc".split("."); System.out.println(Arrays.toString(split)); // 结果为[] String[] split1 = "a|ab|abc".split("|"); System.out.println(Arrays.toString(split1)); // 结果为["a", "|", "a", "b", "|", "a", "b", "c"]
正例:
// String.split(String regex) 正例 // . 需要转译 String[] split2 = "a.ab.abc".split("\."); System.out.println(Arrays.toString(split2)); // 结果为["a", "ab", "abc"] // | 需要转译 String[] split3 = "a|ab|abc".split("\|"); System.out.println(Arrays.toString(split3)); // 结果为["a", "ab", "abc"]
图2. String.split(String regex) 正反例
相关文章
- 在 Go 里用 CGO?这 7 个问题你要关注!
- 9款优秀的去中心化通讯软件 Matrix 的客户端
- 求职数据分析,项目经验该怎么写
- 在OKR中,我看到了数据驱动业务的未来
- 火山引擎云原生大数据在金融行业的实践
- OpenHarmony富设备移植指南(二)—从postmarketOS获取移植资源
- 《数据成熟度指数》报告:64%的企业领袖认为大多数员工“不懂数据”
- OpenHarmony 小型系统兼容性测试指南
- 肯睿中国(Cloudera):2023年企业数字战略三大趋势预测
- 适用于 Linux 的十大命令行游戏
- GNOME 截图工具的新旧截图方式
- System76 即将推出的 COSMIC 桌面正在酝酿大变化
- 2GB 内存 8GB 存储即可流畅运行,Windows 11 极致精简版系统 Tiny11 发布
- 迎接 ecode:一个即将推出的具有全新图形用户界面框架的现代、轻量级代码编辑器
- loongarch架构介绍(三)—地址翻译
- Go 语言怎么解决编译器错误“err is shadowed during return”?
- 敏捷:可能被开发人员遗忘的部分
- Denodo预测2023年数据管理和分析的未来
- 利用数据推动可持续发展
- 在 Vue3 中实现 React 原生 Hooks(useState、useEffect),深入理解 React Hooks 的