jwenjian / ghiblog

GitHub Issues Blog, powered by GitHub Issues and GitHub Actions

Home Page:https://1link.fun

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

消灭 Java 代码的坏味道-InfoQ

jwenjian opened this issue · comments

消灭 Java 代码的坏味道-InfoQ

  • 需要 Map 的主键和取值时,应该迭代 entrySet()
  • 应该使用 Collection.isEmpty() 检测空

  • 不要把集合对象传给自己
  • 集合初始化尽量指定大小
  • 字符串拼接使用 StringBuilder
  • List 的随机访问
  • 频繁调用 Collection.contains 方法请使用 Set
  • 长整型常量后添加大写 L
  • 不要使用魔法值
  • 不要使用集合实现来赋值静态成员变量
  • 建议使用 try-with-resources 语句
  • 删除未使用的私有方法和字段
  • 删除未使用的局部变量
  • 删除未使用的方法参数
  • 删除表达式的多余括号
  • 工具类应该屏蔽构造函数
  • 删除多余的异常捕获并抛出
  • 公有静态常量应该通过类访问
  • 不要用 NullPointerException 判断空
  • 使用 String.valueOf(value) 代替 ""+value
  • 过时代码添加 @deprecated 注解
  • 禁止使用构造方法 BigDecimal(double)
  • 返回空数组和空集合而不是 null
  • 优先使用常量或确定值来调用 equals 方法
  • 枚举的属性字段必须是私有不可变
  • 小心 String.split(String regex)

原文链接 -> https://www.infoq.cn/article/dwA5RVr96s1VtA75ltwz