导航
首页
开发相关
后端技术
工具资源
随笔
其他
总结
留言板
如何更规范化编写Java 代码
转载
发布于
2021-08-25 12:51:24
|
后端技术
| 浏览(
91
) | 评论(
0
)
<p><em><strong>背景:</strong></em>如何更规范化编写Java 代码的重要性想必毋需多言,其中最重要的几点当属提高代码性能、使代码远离Bug、令代码更优雅。</p> <p><strong><em>一、MyBatis 不要为了多个查询条件而写 1 = 1</em></strong></p> <p> 当遇到多个查询条件,使用where 1=1 可以很方便的解决我们的问题,但是这样很可能会造成非常大的性能损失,因为添加了 “where 1=1 ”的过滤条件之后,数据库系统就无法使用索引等查询优化策略,数据库系统将会被迫对每行数据进行扫描(即全表扫描) 以比较此行是否满足过滤条件,当表中的数据量较大时查询速度会非常慢;此外,还会存在SQL 注入的风险。</p> <p><strong><em>反例:</em></strong></p> <pre> <code class="language-xml"><select id="queryBookInfo" parameterType="com.tjt.platform.entity.BookInfo" resultType="java.lang.Integer"> select count(*) from t_rule_BookInfo t where 1=1 <if test="title !=null and title !='' "> AND title = #{title} </if> <if test="author !=null and author !='' "> AND author = #{author} </if> </select></code></pre> <p> </p> <p><strong><em>正例:</em></strong></p> <pre> <code class="language-xml"><select id="queryBookInfo" parameterType="com.tjt.platform.entity.BookInfo" resultType="java.lang.Integer"> select count(*) from t_rule_BookInfo t <where> <if test="title !=null and title !='' "> title = #{title} </if> <if test="author !=null and author !='' "> AND author = #{author} </if> </where> </select></code></pre> <p> </p> <p>UPDATE 操作也一样,可以用<set> 标记代替 1=1。</p> <p><em><strong>二、 迭代entrySet() 获取Map 的key 和value</strong></em></p> <p> 当循环中只需要获取Map 的主键key时,迭代keySet() 是正确的;但是,当需要主键key 和取值value 时,迭代entrySet() 才是更高效的做法,其比先迭代keySet() 后再去通过get 取值性能更佳。</p> <p><strong><em>反例:</em></strong></p> <pre> <code class="language-java">//Map 获取value 反例: HashMap<String, String> map = new HashMap<>(); for (String key : map.keySet()){ String value = map.get(key); }</code></pre> <p><em><strong>正例:</strong></em></p> <pre> <code class="language-java">//Map 获取key & value 正例: HashMap<String, String> map = new HashMap<>(); for (Map.Entry<String,String> entry : map.entrySet()){ String key = entry.getKey(); String value = entry.getValue(); }</code></pre> <p><strong><em>三、使用Collection.isEmpty() 检测空</em></strong></p> <p> 使用Collection.size() 来检测是否为空在逻辑上没有问题,但是使用Collection.isEmpty() 使得代码更易读,并且可以获得更好的性能;除此之外,任何Collection.isEmpty() 实现的时间复杂度都是O(1) ,不需要多次循环遍历,但是某些通过Collection.size() 方法实现的时间复杂度可能是O(n)。<a href="https://www.cnblogs.com/taojietaoge/p/10947214.html">O(1)纬度减少循环次数 例子</a></p> <p><em><strong>反例:</strong></em></p> <pre> <code class="language-java">LinkedList<Object> collection = new LinkedList<>(); if (collection.size() == 0){ System.out.println("collection is empty."); }</code></pre> <p><strong><em>正例:</em></strong></p> <pre> <code class="language-java">LinkedList<Object> collection = new LinkedList<>(); if (collection.isEmpty()){ System.out.println("collection is empty."); } //检测是否为null 可以使用CollectionUtils.isEmpty() if (CollectionUtils.isEmpty(collection)){ System.out.println("collection is null."); }</code></pre> <p> </p> <p><strong><em>四、初始化集合时尽量指定其大小</em></strong></p> <p> 尽量在初始化时指定集合的大小,能有效减少集合的扩容次数,因为集合每次扩容的时间复杂度很可能时O(n),耗费时间和性能。</p> <p><em><strong>反例:</strong></em></p> <pre> <code class="language-java">//初始化list,往list 中添加元素反例: int[] arr = new int[]{1,2,3,4}; List<Integer> list = new ArrayList<>(); for (int i : arr){ list.add(i); }</code></pre> <p><strong><em>正例:</em></strong></p> <pre> <code class="language-java">//初始化list,往list 中添加元素正例: int[] arr = new int[]{1,2,3,4}; //指定集合list 的容量大小 List<Integer> list = new ArrayList<>(arr.length); for (int i : arr){ list.add(i); }</code></pre> <p> </p> <p><em><strong>五、使用StringBuilder 拼接字符串</strong></em></p> <p> 一般的字符串拼接在编译期Java 会对其进行优化,但是在循环中字符串的拼接Java 编译期无法执行优化,所以需要使用StringBuilder 进行替换。</p> <p><em><strong>反例:</strong></em></p> <pre> <code class="language-java">//在循环中拼接字符串反例 String str = ""; for (int i = 0; i < 10; i++){ //在循环中字符串拼接Java 不会对其进行优化 str += i; }</code></pre> <p><strong><em>正例:</em></strong></p> <pre> <code class="language-java">//在循环中拼接字符串正例 String str1 = "Love"; String str2 = "Courage"; String strConcat = str1 + str2; //Java 编译器会对该普通模式的字符串拼接进行优化 StringBuilder sb = new StringBuilder(); for (int i = 0; i < 10; i++){ //在循环中,Java 编译器无法进行优化,所以要手动使用StringBuilder sb.append(i); }</code></pre> <p> </p> <p><em><strong> 六、若需频繁调用Collection.contains 方法则使用Set</strong></em></p> <p> 在Java 集合类库中,List的contains 方法普遍时间复杂度为O(n),若代码中需要频繁调用contains 方法查找数据则先将集合list 转换成HashSet 实现,将O(n) 的时间复杂度将为O(1)。</p> <p><em><strong>反例:</strong></em></p> <pre> <code class="language-java">//频繁调用Collection.contains() 反例 List<Object> list = new ArrayList<>(); for (int i = 0; i <= Integer.MAX_VALUE; i++){ //时间复杂度为O(n) if (list.contains(i)) System.out.println("list contains "+ i); }</code></pre> <p><strong><em>正例:</em></strong></p> <pre> <code class="language-java">//频繁调用Collection.contains() 正例 List<Object> list = new ArrayList<>(); Set<Object> set = new HashSet<>(); for (int i = 0; i <= Integer.MAX_VALUE; i++){ //时间复杂度为O(1) if (set.contains(i)){ System.out.println("list contains "+ i); } }</code></pre> <p><em><strong>七、使用静态代码块实现赋值静态成员变量</strong></em></p> <p> 对于集合类型的静态成员变量,应该使用静态代码块赋值,而不是使用集合实现来赋值。</p> <p><em><strong>反例:</strong></em></p> <pre> //赋值静态成员变量反例 </pre> <pre> <code class="language-java">private static Map<String, Integer> map = new HashMap<String, Integer>(){ { map.put("Leo",1); map.put("Family-loving",2); map.put("Cold on the out side passionate on the inside",3); } }; private static List<String> list = new ArrayList<>(){ { list.add("Sagittarius"); list.add("Charming"); list.add("Perfectionist"); } };</code></pre> <p><strong><em>正例:</em></strong></p> <pre> <code class="language-java">//赋值静态成员变量正例 private static Map<String, Integer> map = new HashMap<String, Integer>(); static { map.put("Leo",1); map.put("Family-loving",2); map.put("Cold on the out side passionate on the inside",3); } private static List<String> list = new ArrayList<>(); static { list.add("Sagittarius"); list.add("Charming"); list.add("Perfectionist"); }</code></pre> <p><strong><em>八、删除未使用的局部变量、方法参数、私有方法、字段和<strong><em>多余的括号</em></strong>。</em></strong></p> <p><em><strong>九、工具类中屏蔽构造函数</strong></em></p> <p> 工具类是一堆静态字段和函数的集合,其不应该被实例化;但是,Java 为每个没有明确定义构造函数的类添加了一个隐式公有构造函数,为了避免不必要的实例化,应该显式定义私有构造函数来屏蔽这个隐式公有构造函数。</p> <p><em><strong>反例:</strong></em></p> <pre> <code class="language-java">public class PasswordUtils { //工具类构造函数反例 private static final Logger LOG = LoggerFactory.getLogger(PasswordUtils.class); public static final String DEFAULT_CRYPT_ALGO = "PBEWithMD5AndDES"; public static String encryptPassword(String aPassword) throws IOException { return new PasswordUtils(aPassword).encrypt(); }</code></pre> <p><strong><em>正例:</em></strong></p> <pre> <code class="language-java">public class PasswordUtils { //工具类构造函数正例 private static final Logger LOG = LoggerFactory.getLogger(PasswordUtils.class); //定义私有构造函数来屏蔽这个隐式公有构造函数 private PasswordUtils(){} public static final String DEFAULT_CRYPT_ALGO = "PBEWithMD5AndDES"; public static String encryptPassword(String aPassword) throws IOException { return new PasswordUtils(aPassword).encrypt(); }</code></pre> <p><em><strong>十、删除多余的异常捕获并抛出</strong></em></p> <p> 用catch 语句捕获异常后,若什么也不进行处理,就只是让异常重新抛出,这跟不捕获异常的效果一样,可以删除这块代码或添加别的处理。</p> <p><em><strong>反例:</strong></em></p> <p> </p> <pre> <code class="language-java">//多余异常反例 private static String fileReader(String fileName)throws IOException{ try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) { String line; StringBuilder builder = new StringBuilder(); while ((line = reader.readLine()) != null) { builder.append(line); } return builder.toString(); } catch (Exception e) { //仅仅是重复抛异常 未作任何处理 throw e; } }</code></pre> <p><strong><em>正例:</em></strong></p> <pre> <code class="language-java">//多余异常正例 private static String fileReader(String fileName)throws IOException{ try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) { String line; StringBuilder builder = new StringBuilder(); while ((line = reader.readLine()) != null) { builder.append(line); } return builder.toString(); //删除多余的抛异常,或增加其他处理: /*catch (Exception e) { return "fileReader exception"; }*/ } }</code></pre> <p><em><strong>十一、字符串转化使用String.valueOf(value) 代替 " " + value</strong></em></p> <p> 把其它对象或类型转化为字符串时,使用String.valueOf(value) 比 ""+value 的效率更高。</p> <p><em><strong>反例:</strong></em></p> <pre> <code class="language-java">//把其它对象或类型转化为字符串反例: int num = 520; // "" + value String strLove = "" + num;</code></pre> <p><strong><em>正例:</em></strong></p> <pre> <code class="language-java">//把其它对象或类型转化为字符串正例: int num = 520; // String.valueOf() 效率更高 String strLove = String.valueOf(num);</code></pre> <p><em><strong>十二、避免使用BigDecimal(double)</strong></em></p> <p> BigDecimal(double) 存在精度损失风险,在精确计算或值比较的场景中可能会导致业务逻辑异常。</p> <p><em><strong>反例:</strong></em></p> <pre> <code class="language-java">// BigDecimal 反例 BigDecimal bigDecimal = new BigDecimal(0.11D);</code></pre> <p><strong><em>正例:</em></strong></p> <pre> <code class="language-java">// BigDecimal 正例 BigDecimal bigDecimal1 = bigDecimal.valueOf(0.11D);</code></pre> <p><strong>图1. BigDecimal 精度丢失</strong></p> <p><img alt="" src="//oss.mtjo.net/img/mtjo/3f9b9ffb671dbd96ddeffc2c23de7c5f.png" /></p> <p><em><strong>十三、返回空数组和集合而非 null</strong></em></p> <p> 若程序运行返回null,需要调用方强制检测null,否则就会抛出空指针异常;返回空数组或空集合,有效地避免了调用方因为未检测null 而抛出空指针异常的情况,还可以删除调用方检测null 的语句使代码更简洁。</p> <p><em><strong>反例:</strong></em></p> <pre> <code class="language-java">//返回null 反例 public static Result[] getResults() { return null; } public static List<Result> getResultList() { return null; } public static Map<String, Result> getResultMap() { return null; }</code></pre> <p><strong><em>正例:</em></strong></p> <pre> <code class="language-java">//返回空数组和空集正例 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(); }</code></pre> <p><strong><em>十四、优先使用常量或确定值调用equals 方法</em></strong></p> <p> 对象的equals 方法容易抛空指针异常,应使用常量或确定有值的对象来调用equals 方法。</p> <p><em><strong>反例:</strong></em></p> <pre> <code class="language-java">//调用 equals 方法反例 private static boolean fileReader(String fileName)throws IOException{ // 可能抛空指针异常 return fileName.equals("Charming"); }</code></pre> <p><strong><em>正例:</em></strong></p> <pre> <code class="language-java">//调用 equals 方法正例 private static boolean fileReader(String fileName)throws IOException{ // 使用常量或确定有值的对象来调用 equals 方法 return "Charming".equals(fileName); //或使用: java.util.Objects.equals() 方法 return Objects.equals("Charming",fileName); }</code></pre> <p><em><strong>十五、枚举的属性字段必须是私有且不可变</strong></em></p> <p> 枚举通常被当做常量使用,如果枚举中存在公共属性字段或设置字段方法,那么这些枚举常量的属性很容易被修改;理想情况下,枚举中的属性字段是私有的,并在私有构造函数中赋值,没有对应的Setter 方法,最好加上final 修饰符。</p> <p><em><strong>反例:</strong></em></p> <pre> <code class="language-java">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; } }</code></pre> <p><strong><em>正例:</em></strong></p> <pre> <code class="language-java">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; } }</code></pre> <p><strong><em>十六、tring.split(String regex)部分关键字需要转译</em></strong></p> <p> 使用字符串String 的plit 方法时,传入的分隔字符串是正则表达式,则部分关键字(比如 .[]()\| 等)需要转义。</p> <p><em><strong>反例:</strong></em></p> <pre> <code class="language-java">// 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"]</code></pre> <p><strong><em>正例:</em></strong></p> <pre> <code class="language-java">// 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"]</code></pre> <p> </p> <p><strong>图2. String.split(String regex) 正反例</strong></p> <p><img alt="" src="//oss.mtjo.net/img/mtjo/240cd986d97dd24b84b65022e6519a2c.png" /></p> <p> </p>
本文转自
https://www.cnblogs.com/taojietaoge/p/11575376.html
,如有侵权,请联系删除。
点赞
3
打赏
微信赞赏
支付宝赞赏
发表评论
欢迎您:
发表评论
最新文章
推荐
点击排行
1
再给领克旧车机一次机会——雷达触发360全景、转向灯触发360全景、汽车事件联动车机、超速提示。。。一样都不能少
2
GO配置国内镜像
3
Java 线程的创建方式有哪些?
4
IOS、Mac中国法定节假日
5
卸载阿里云监控软件方法
6
iftop – 实时 Linux 网络带宽监控工具
7
免费申请和使用IntelliJ IDEA商业版License指南
8
ssh 远程执行命令
9
使用 systemd 限制系统资源的使用
10
如何更规范化编写Java 代码
1
再给领克旧车机一次机会——雷达触发360全景、转向灯触发360全景、汽车事件联动车机、超速提示。。。一样都不能少
2
Mac OS 最好用的鼠标手势软件 MacStroke
3
git 找回丢失的commit
4
小米路由器插件
5
Redis中的批量删除数据库中的Key
6
LINUX 安装多个版本JDK
7
免费申请和使用IntelliJ IDEA商业版License指南
8
精简 迅雷 for MAC
9
Dnsmasq 配置文件详解
10
Nginx深入详解之upstream分配方式
1
小米路由器插件
2
基于Android Webview的Hybrid App开发的前端优化
3
LINUX 安装多个版本JDK
4
Android Studio使用心得 - 常见问题
5
XDebug 调试PHP 配置
6
mysql主从复制
7
Git忽略规则及.gitignore规则不生效的解决办法
8
如何在一台机器上配置多个git的rsa
9
关于Android与pc通信时中文乱码的分析和解决
10
MacStroke
标签云
Linux
Java
Spring
Spring Boot
其他
IntelliJ IDEA
MacOS
工具
资源
JavaScript
Mac
网站信息
浏览总数:
629552
文章总数:
47
篇
标签总数:
11
个
分类总数:
6
个
留言数量:
10
条
关于本站
MTJO
明天见哦
Copyright © 2022. MTJO ·
桂ICP备16000489号-1
Powered by
MTJO