偏执Java编程的技术
文章目录
为什么我们要采用“防御性编程”,即一些偏执习惯的原因。下面是我个人认为的几个最有用但偏执的Java编程技术。一起来看看吧:
1.将String字符串放在最前面
为了防止偶发性的NullPointerException 异常,我们通常将String放置在equals()函数的左边来实现字符串比较,如下代码:
代码清单1:
- // Bad
- if (variable.equals("literal")) { ... }
- // Good
- if ("literal".equals(variable)) { ... }
[color=rgb(255, 178, 1) !important]复制代码
2.不要相信早期的JDK API
代码清单2:
- String[] files = file.list();
- // Look
- if (files != null) {
- for (int i = 0; i < files.length; i++) {
- ...
- }
- }
[color=rgb(255, 178, 1) !important]复制代码
看上去很偏执?请看Javadoc:
如果这个虚拟路径不表示一个文件夹目录,则此方法返回null。否则将会返回一个字符串数组,每一个字符串表示目录中的文件或文件夹。
所以,我们可以添加一些校验:
代码清单3:
- if (file.isDirectory()) {
- String[] files = file.list();
- // Look
- if (files != null) {
- for (int i = 0; i < files.length; i++) {
- ...
- }
- }
- }
[color=rgb(255, 178, 1) !important]复制代码
3.不要相信“-1”
Javadoc中对 String.indexOf()方法明确指出:对象内第一次出现指定字符的位置索引,如果为-1则表示该字符不在字符序列中。
所以使用-1是理所当然的,对吗?我说不对,请看以下代码:
代码清单4:
- // Bad
- if (string.indexOf(character) != -1) { ... }
- // Good
- if (string.indexOf(character) >= 0) { ... }
[color=rgb(255, 178, 1) !important]复制代码
如果到时候他们改变了编码方式,对字符串并不区分大小写,也许更好的方式是返回-2呢?
4.避免意外赋值
代码清单5:
- if (variable = 5) { ... }
- if (5 = variable) { ... }
- if (5 === variable) { ... }
[color=rgb(255, 178, 1) !important]复制代码
所以你可以将比较常量放置在左侧,这样就不会发生意外赋值的错误了。
5.检查Null和Length
无论如何,只要你有一个集合、数组等,请确保它存在,并且不为空。
代码清单6:
- // Bad
- if (array.length > 0) { ... }
- // Good
- if (array != null && array.length > 0) { ... }
[color=rgb(255, 178, 1) !important]复制代码
6.所有的方法都是final的
你也许会告诉我你的开/闭原则,但我不相信你(正确继承我这个父类的所有子类),我连我自己(不小心继承我这个父类的所有子类)都相信,怎么可能相信你!所以对于那些意义明确的方法要严格用final标识。
代码清单7:
- // Bad
- public void boom() { ... }
- // Good. Don't touch.
- public final void dontTouch() { ... }
[color=rgb(255, 178, 1) !important]复制代码
7.所有变量和参数都是final
就像我说的,我不相信我自己(不要意外覆盖我的值)。
代码清单8:
- // Bad
- void input(String importantMessage) {
- String answer = "...";
- answer = importantMessage = "LOL accident";
- }
- // Good
- final void input(final String importantMessage) {
- final String answer = "...";
- }
[color=rgb(255, 178, 1) !important]复制代码
8.重载时不要相信泛型
你相信你写的超级骚气的API,它很直观,随之而来的,一些用户谁只是将原始类型转换成Object类型,直到那该死的编译器停止发牢骚,并且突然他们会链接错误的方法,而却以为这是你的错误。
代码清单9:
- // Bad
- <T> void bad(T value) {
- bad(Collections.singletonList(value));
- }
- <T> void bad(List<T> values) {
- ...
- }
- // Good
- final <T> void good(final T value) {
- if (value instanceof List)
- good((List<?>) value);
- else
- good(Collections.singletonList(value));
- }
- final <T> void good(final List<T> values) {
- ...
- }
[color=rgb(255, 178, 1) !important]复制代码
因为,你知道,你的用户,他们就像……
代码清单10:
- @SuppressWarnings("all")
- Object t = (Object) (List) Arrays.asList("abc");
- bad(t);<span > </span>
[color=rgb(255, 178, 1) !important]复制代码
相信我,又有可能碰到下面的情况:
所以,这种偏执还是不错的。
9.总是在Switch语句的Default中抛出异常
代码清单11:
- // Bad
- switch (value) {
- case 1: foo(); break;
- case 2: bar(); break;
- }
- // Good
- switch (value) {
- case 1: foo(); break;
- case 2: bar(); break;
- default:
- throw new ThreadDeath("That'll teach them");
- }
[color=rgb(255, 178, 1) !important]复制代码
当value == 3时,将会出现无法找到的提示,而不会让人不知所谓。
10.Switch语句带花括号
代码清单12:
- // Bad
- switch (value) {
- case 1: int j = 1; break;
- case 2: int j = 2; break;
- }
- // Good
- switch (value) {
- case 1: {
- final int j = 1;
- break;
- }
- case 2: {
- final int j = 2;
- break;
- }
- // Remember:
- default:
- throw new ThreadDeath("That'll teach them");
- }
[color=rgb(255, 178, 1) !important]复制代码
在switch语句中,每一个case语句的范围只有一行语句,事实上,这些case语句甚至不是真正的语句,他们就像goto语句中的跳转标记一样。
结论
偏执编程看起来似乎不可思议,有时,因为代码经常被证明是更详细一点,但并不是需求需要。你可能会想,“哦,这是绝不会发生的”,但正如我所说,只有踩过坑的才能知道这些偏执的重要性!

评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论