1. 首页
  2. IT资讯

当我遵循了这 16 条规范写代码,同事只对我说了三个字:666

“u003Cimg src=”http:u002Fu002Fp1.pstatp.comu002Flargeu002Fpgc-imageu002FRf8n3jdBqecfy8″ img_width=”1080″ img_height=”345″ alt=”当我遵循了这 16 条规范写代码,同事只对我说了三个字:666″ inline=”0″u003Eu003Cpu003E作者 | 涛姐u003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-1″u003E涛哥u003Cu002Fiu003Eu003Cu002Fpu003Eu003Cpu003E链接 | cnblogsu003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-6″u003E.comu003Cu002Fiu003Eu002Ftaojietaogeu002Fpu002F1u003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-6″u003E157u003Cu002Fiu003E5376.htmlu003Cu002Fpu003Eu003Cpu003E如何更规范化编写Java 代码u003Cu002Fpu003Eu003Cpu003EMany of the happiest people are those who own the least. But are we really so happy with our IPhones, our big houses, our fancy cars?u003Cu002Fpu003Eu003Cpu003E忘川如斯,拥有一切的人才更怕失去。u003Cu002Fpu003Eu003Cpu003E背景:如何更规范化编写Java 代码的重要性想必毋需多言,其中最重要的几点当属提高代码性能、使代码远离Bug、令代码更优雅。u003Cu002Fpu003Eu003Cpu003Eu003Cstrongu003E一、MyBatis 不要为了多个查询条件而写 1 = 1u003Cu002Fstrongu003Eu003Cu002Fpu003Eu003Cpu003E当遇到多个查询条件,使用where 1=1 可以很方便的解决我们的问题,但是这样很可能会造成非常大的性能损失,因为添加了 “where 1=1 ”的过滤条件之后,数据库系统就无法使用索引等查询优化策略,数据库系统将会被迫对每行数据进行扫描(即全表扫描) 以比较此行是否满足过滤条件,当表中的数据量较大时查询速度会非常慢;此外,还会存在SQL 注入的风险。u003Cu002Fpu003Eu003Cpu003E反例:u003Cu002Fpu003Eu003Cpreu003Eu003Ccodeu003E<select id=”queryBookInfo” parameterType=”com.tjt.platform.entity.BookInfo” resultType=”java.lang.Integer”>u003Cu002Fcodeu003Eu003Ccodeu003E select count(*) from t_rule_BookInfo t where 1=1u003Cu002Fcodeu003Eu003Ccodeu003E<if test=”title != and title !=” “>u003Cu002Fcodeu003Eu003Ccodeu003E AND title = #{title} u003Cu002Fcodeu003Eu003Ccodeu003E<u002Fif> u003Cu002Fcodeu003Eu003Ccodeu003E<if test=”author != and author !=” “>u003Cu002Fcodeu003Eu003Ccodeu003E AND author = #{author}u003Cu002Fcodeu003Eu003Ccodeu003E<u002Fif> u003Cu002Fcodeu003Eu003Ccodeu003E<u002Fselect>u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003E正例:u003Cu002Fpu003Eu003Cpreu003Eu003Ccodeu003E<select id=”queryBookInfo” parameterType=”com.tjt.platform.entity.BookInfo” resultType=”java.lang.Integer”>u003Cu002Fcodeu003Eu003Ccodeu003E select count(*) from t_rule_BookInfo tu003Cu002Fcodeu003Eu003Ccodeu003E<where>u003Cu002Fcodeu003Eu003Ccodeu003E<if test=”title != and title !=” “>u003Cu002Fcodeu003Eu003Ccodeu003E title = #{title} u003Cu002Fcodeu003Eu003Ccodeu003E<u002Fif>u003Cu002Fcodeu003Eu003Ccodeu003E<if test=”author != and author !=” “> u003Cu002Fcodeu003Eu003Ccodeu003E AND author = #{author}u003Cu002Fcodeu003Eu003Ccodeu003E<u002Fif>u003Cu002Fcodeu003Eu003Ccodeu003E<u002Fwhere> u003Cu002Fcodeu003Eu003Ccodeu003E<u002Fselect>u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003EUPDATE 操作也一样,可以用<set> 标记代替 1=1。u003Cu002Fpu003Eu003Cpu003Eu003Cstrongu003E二、 迭代entrySet 获取Map 的key 和valueu003Cu002Fstrongu003Eu003Cu002Fpu003Eu003Cpu003E当循环中只需要获取Map 的主键key时,迭代keySet 是正确的;但是,当需要主键key 和取值value 时,迭代entrySet 才是更高效的做法,其比先迭代keySet 后再去通过get 取值性能更佳。u003Cu002Fpu003Eu003Cpu003E反例:u003Cu002Fpu003Eu003Cpreu003Eu003Ccodeu003Eu002Fu002FMap 获取value 反例:u003Cu002Fcodeu003Eu003Ccodeu003EHashMap<String, String> map = new HashMap<>;u003Cu002Fcodeu003Eu003Ccodeu003Efor (String key : map.keySet){u003Cu002Fcodeu003Eu003Ccodeu003EString value = map.get(key);u003Cu002Fcodeu003Eu003Ccodeu003E}u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003E正例:u003Cu002Fpu003Eu003Cpreu003Eu003Ccodeu003E u002Fu002FMap 获取key & value 正例:u003Cu002Fcodeu003Eu003Ccodeu003EHashMap<String, String> map = new HashMap<>;u003Cu002Fcodeu003Eu003Ccodeu003E for (Map.Entry<String,String> entry : map.entrySet){u003Cu002Fcodeu003Eu003Ccodeu003E String key = entry.getKey;u003Cu002Fcodeu003Eu003Ccodeu003E String value = entry.getValue;u003Cu002Fcodeu003Eu003Ccodeu003E}u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003Eu003Cstrongu003E三、使用Collection.isEmpty 检测空u003Cu002Fstrongu003Eu003Cu002Fpu003Eu003Cpu003E使用Collection.size 来检测是否为空在逻辑上没有问题,但是使用Collection.isEmpty 使得代码更易读,并且可以获得更好的性能;除此之外,任何Collection.isEmpty 实现的时间复杂度都是O(1) ,不需要多次循环遍历,但是某些通过Collection.size 方法实现的时间复杂度可能是O(n)。O(1)纬度减少循环次数 例子u003Cu002Fpu003Eu003Cpu003E反例:u003Cu002Fpu003Eu003Cpreu003Eu003Ccodeu003ELinkedList<Object> collection = new LinkedList<>;u003Cu002Fcodeu003Eu003Ccodeu003Eif (collection.size == 0){u003Cu002Fcodeu003Eu003Ccodeu003E System.out.println(“collection is empty.”);u003Cu002Fcodeu003Eu003Ccodeu003E }u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003E正例:u003Cu002Fpu003Eu003Cpreu003Eu003Ccodeu003ELinkedList<Object> collection = new LinkedList<>;u003Cu002Fcodeu003Eu003Ccodeu003E if (collection.isEmpty){u003Cu002Fcodeu003Eu003Ccodeu003E System.out.println(“collection is empty.”);u003Cu002Fcodeu003Eu003Ccodeu003E }u003Cu002Fcodeu003Eu003Ccodeu003E u002Fu002F检测是否为 可以使用CollectionUtils.isEmptyu003Cu002Fcodeu003Eu003Ccodeu003E if (CollectionUtils.isEmpty(collection)){u003Cu002Fcodeu003Eu003Ccodeu003E System.out.println(“collection is .”);u003Cu002Fcodeu003Eu003Cbru003Eu003Ccodeu003E }u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003Eu003Cstrongu003E四、初始化集合时尽量指定其大小u003Cu002Fstrongu003Eu003Cu002Fpu003Eu003Cpu003E尽量在初始化时指定集合的大小,能有效减少集合的扩容次数,因为集合每次扩容的时间复杂度很可能时O(n),耗费时间和性能。u003Cu002Fpu003Eu003Cpu003E反例:u003Cu002Fpu003Eu003Cpreu003Eu003Cdivu003Eu003Cpreu003Eu003Ccodeu003Eu002Fu002F初始化list,往list 中添加元素反例:u003Cu002Fcodeu003Eu003Ccodeu003Eint arr = new int{1,2,3,4};u003Cu002Fcodeu003Eu003Ccodeu003EList<Integer> list = new ArrayList<>;u003Cu002Fcodeu003Eu003Ccodeu003Efor (int i : arr){u003Cu002Fcodeu003Eu003Ccodeu003E list.au003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-2″u003Eddu003Cu002Fiu003E(i);u003Cu002Fcodeu003Eu003Ccodeu003E}u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cu002Fdivu003Eu003Cu002Fpreu003Eu003Cpu003E正例:u003Cu002Fpu003Eu003Cpreu003Eu003Ccodeu003Eu002Fu002F初始化list,往list 中添加元素正例:u003Cu002Fcodeu003Eu003Ccodeu003E int arr = new int{1,2,3,4};u003Cu002Fcodeu003Eu003Ccodeu003E u002Fu002F指定集合list 的容量大小u003Cu002Fcodeu003Eu003Ccodeu003E List<Integer> list = new ArrayList<>(arr.length);u003Cu002Fcodeu003Eu003Ccodeu003E for (int i : arr){u003Cu002Fcodeu003Eu003Ccodeu003E list.au003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-2″u003Eddu003Cu002Fiu003E(i);u003Cu002Fcodeu003Eu003Ccodeu003E }u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003Eu003Cstrongu003E五、使用StringBuilder 拼接字符串u003Cu002Fstrongu003Eu003Cu002Fpu003Eu003Cpu003E一般的字符串拼接在编译期Java 会对其进行优化,但是在循环中字符串的拼接Java 编译期无法执行优化,所以需要使用StringBuilder 进行替换。u003Cu002Fpu003Eu003Cpu003E反例:u003Cu002Fpu003Eu003Cpreu003Eu003Cdivu003Eu003Cpreu003Eu003Ccodeu003Eu002Fu002F在循环中拼接字符串反例u003Cu002Fcodeu003Eu003Ccodeu003EString str = “”;u003Cu002Fcodeu003Eu003Ccodeu003Efor (int i = 0; i < 10; i++){u003Cu002Fcodeu003Eu003Ccodeu003E u002Fu002F在循环中字符串拼接Java 不会对其进行优化u003Cu002Fcodeu003Eu003Ccodeu003E str += i;u003Cu002Fcodeu003Eu003Ccodeu003E}u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cu002Fdivu003Eu003Cu002Fpreu003Eu003Cpu003E正例:u003Cu002Fpu003Eu003Cpreu003Eu003Ccodeu003Eu002Fu002F在循环中拼接字符串正例u003Cu002Fcodeu003Eu003Ccodeu003E String str1 = “Love”;u003Cu002Fcodeu003Eu003Ccodeu003E String str2 = “Courage”;u003Cu002Fcodeu003Eu003Ccodeu003E String strConcat = str1 + str2; u002Fu002FJava 编译器会对该普通模式的字符串拼接进行优化u003Cu002Fcodeu003Eu003Ccodeu003E StringBuilder sb = new StringBuilder;u003Cu002Fcodeu003Eu003Ccodeu003E for (int i = 0; i < 10; i++){u003Cu002Fcodeu003Eu003Ccodeu003E u002Fu002F在循环中,Java 编译器无法进行优化,所以要手动使用StringBuilderu003Cu002Fcodeu003Eu003Ccodeu003E sb.append(i);u003Cu002Fcodeu003Eu003Ccodeu003E }u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003Eu003Cstrongu003E六、若需频繁调用Collection.contains 方法则使用Setu003Cu002Fstrongu003Eu003Cu002Fpu003Eu003Cpu003E在Java 集合类库中,List的contains 方法普遍时间复杂度为O(n),若代码中需要频繁调用contains 方法查找数据则先将集合list 转换成HashSet 实现,将O(n) 的时间复杂度将为O(1)。u003Cu002Fpu003Eu003Cpu003E反例:u003Cu002Fpu003Eu003Cpreu003Eu003Cdivu003Eu003Cpreu003Eu003Ccodeu003Eu002Fu002F频繁调用Collection.contains 反例u003Cu002Fcodeu003Eu003Ccodeu003EList<Object> list = new ArrayList<>;u003Cu002Fcodeu003Eu003Ccodeu003Efor (int i = 0; i <= Integer.MAX_VALUE; i++){u003Cu002Fcodeu003Eu003Ccodeu003E u002Fu002F时间复杂度为O(n)u003Cu002Fcodeu003Eu003Ccodeu003E if (list.contains(i))u003Cu002Fcodeu003Eu003Ccodeu003E System.out.println(“list contains “+ i);u003Cu002Fcodeu003Eu003Ccodeu003E }u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cu002Fdivu003Eu003Cu002Fpreu003Eu003Cpu003E正例:u003Cu002Fpu003Eu003Cpreu003Eu003Ccodeu003Eu002Fu002F频繁调用Collection.contains 正例u003Cu002Fcodeu003Eu003Ccodeu003E List<Object> list = new ArrayList<>;u003Cu002Fcodeu003Eu003Ccodeu003E Set<Object> set = new HashSet<>;u003Cu002Fcodeu003Eu003Ccodeu003E for (int i = 0; i <= Integer.MAX_VALUE; i++){u003Cu002Fcodeu003Eu003Ccodeu003E u002Fu002F时间复杂度为O(1)u003Cu002Fcodeu003Eu003Ccodeu003E if (set.contains(i)){u003Cu002Fcodeu003Eu003Ccodeu003E System.out.println(“list contains “+ i);u003Cu002Fcodeu003Eu003Ccodeu003E }u003Cu002Fcodeu003Eu003Ccodeu003E }u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003Eu003Cstrongu003E七、使用静态代码块实现赋值静态成员变量u003Cu002Fstrongu003Eu003Cu002Fpu003Eu003Cpu003E对于集合类型的静态成员变量,应该使用静态代码块赋值,而不是使用集合实现来赋值。u003Cu002Fpu003Eu003Cpu003E反例:u003Cu002Fpu003Eu003Cpreu003Eu003Ccodeu003Eu002Fu002F赋值静态成员变量反例u003Cu002Fcodeu003Eu003Ccodeu003E private static Map<String, Integer> map = new HashMap<String, Integer>{u003Cu002Fcodeu003Eu003Ccodeu003E {u003Cu002Fcodeu003Eu003Ccodeu003E map.put(“Leo”,1);u003Cu002Fcodeu003Eu003Ccodeu003E map.put(“Family-loving”,2);u003Cu002Fcodeu003Eu003Ccodeu003E map.put(“Cold on the out side passionate on the inside”,3);u003Cu002Fcodeu003Eu003Ccodeu003E }u003Cu002Fcodeu003Eu003Ccodeu003E };u003Cu002Fcodeu003Eu003Ccodeu003E private static List<String> list = new ArrayList<>{u003Cu002Fcodeu003Eu003Ccodeu003E {u003Cu002Fcodeu003Eu003Ccodeu003E list.au003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-2″u003Eddu003Cu002Fiu003E(“Sagittarius”);u003Cu002Fcodeu003Eu003Ccodeu003E list.au003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-2″u003Eddu003Cu002Fiu003E(“Charming”);u003Cu002Fcodeu003Eu003Ccodeu003E list.au003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-2″u003Eddu003Cu002Fiu003E(“Perfectionist”);u003Cu002Fcodeu003Eu003Ccodeu003E }u003Cu002Fcodeu003Eu003Ccodeu003E };u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003E正例:u003Cu002Fpu003Eu003Cpreu003Eu003Ccodeu003Eu002Fu002F赋值静态成员变量正例u003Cu002Fcodeu003Eu003Ccodeu003Eprivate static Map<String, Integer> map = new HashMap<String, Integer>;u003Cu002Fcodeu003Eu003Ccodeu003E static {u003Cu002Fcodeu003Eu003Ccodeu003E map.put(“Leo”,1);u003Cu002Fcodeu003Eu003Ccodeu003E map.put(“Family-loving”,2);u003Cu002Fcodeu003Eu003Ccodeu003E map.put(“Cold on the out side passionate on the inside”,3);u003Cu002Fcodeu003Eu003Ccodeu003E }u003Cu002Fcodeu003Eu003Ccodeu003Eprivate static List<String> list = new ArrayList<>;u003Cu002Fcodeu003Eu003Ccodeu003E static {u003Cu002Fcodeu003Eu003Ccodeu003E list.au003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-2″u003Eddu003Cu002Fiu003E(“Sagittarius”);u003Cu002Fcodeu003Eu003Ccodeu003E list.au003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-2″u003Eddu003Cu002Fiu003E(“Charming”);u003Cu002Fcodeu003Eu003Ccodeu003E list.au003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-2″u003Eddu003Cu002Fiu003E(“Perfectionist”);u003Cu002Fcodeu003Eu003Ccodeu003E }u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003Eu003Cstrongu003E八、删除未使用的局部变量、方法参数、私有方法、字段和多余的括号。u003Cu002Fstrongu003Eu003Cu002Fpu003Eu003Cpu003Eu003Cstrongu003E九、工具类中屏蔽构造函数u003Cu002Fstrongu003Eu003Cu002Fpu003Eu003Cpu003E工具类是一堆静态字段和函数的集合,其不应该被实例化;但是,Java 为每个没有明确定义构造函数的类添加了一个隐式公有构造函数,为了避免不必要的实例化,应该显式定义私有构造函数来屏蔽这个隐式公有构造函数。u003Cu002Fpu003Eu003Cpu003E反例:u003Cu002Fpu003Eu003Cpreu003Eu003Ccodeu003Epublic class PasswordUtils {u003Cu002Fcodeu003Eu003Ccodeu003E u002Fu002F工具类构造函数反例u003Cu002Fcodeu003Eu003Ccodeu003E private static final Logger LOG = LoggerFactory.getLogger(PasswordUtils.class);u003Cu002Fcodeu003Eu003Cbru003Eu003Ccodeu003E public static final String DEFAULT_CRYPT_ALGO = “PBEWithMD5AndDES”;u003Cu002Fcodeu003Eu003Cbru003Eu003Ccodeu003E public static String encryptPassword(String aPassword) throws IOException {u003Cu002Fcodeu003Eu003Ccodeu003E return new PasswordUtils(aPassword).encrypt;u003Cu002Fcodeu003Eu003Ccodeu003E }u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003E正例:u003Cu002Fpu003Eu003Cpreu003Eu003Ccodeu003Epublic class PasswordUtils {u003Cu002Fcodeu003Eu003Ccodeu003E u002Fu002F工具类构造函数正例u003Cu002Fcodeu003Eu003Ccodeu003E private static final Logger LOG = LoggerFactory.getLogger(PasswordUtils.class);u003Cu002Fcodeu003Eu003Cbru003Eu003Ccodeu003E u002Fu002F定义私有构造函数来屏蔽这个隐式公有构造函数u003Cu002Fcodeu003Eu003Ccodeu003E private PasswordUtils{}u003Cu002Fcodeu003Eu003Ccodeu003E public static final String DEFAULT_CRYPT_ALGO = “PBEWithMD5AndDES”;u003Cu002Fcodeu003Eu003Cbru003Eu003Ccodeu003E public static String encryptPassword(String aPassword) throws IOException {u003Cu002Fcodeu003Eu003Ccodeu003E return new PasswordUtils(aPassword).encrypt;u003Cu002Fcodeu003Eu003Ccodeu003E }u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003Eu003Cstrongu003E十、删除多余的异常捕获并抛出u003Cu002Fstrongu003Eu003Cu002Fpu003Eu003Cpu003E用catch 语句捕获异常后,若什么也不进行处理,就只是让异常重新抛出,这跟不捕获异常的效果一样,可以删除这块代码或添加别的处理。u003Cu002Fpu003Eu003Cpu003E反例:u003Cu002Fpu003Eu003Cpreu003Eu003Ccodeu003Eu002Fu002F多余异常反例u003Cu002Fcodeu003Eu003Ccodeu003Eprivate static String fileReader(String fileName)throws IOException{u003Cu002Fcodeu003Eu003Cbru003Eu003Ccodeu003E try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {u003Cu002Fcodeu003Eu003Ccodeu003E String line;u003Cu002Fcodeu003Eu003Ccodeu003E StringBuilder builder = new StringBuilder;u003Cu002Fcodeu003Eu003Ccodeu003E while ((line = reader.readLine) != ) {u003Cu002Fcodeu003Eu003Ccodeu003E builder.append(line);u003Cu002Fcodeu003Eu003Ccodeu003E }u003Cu002Fcodeu003Eu003Ccodeu003E return builder.toString;u003Cu002Fcodeu003Eu003Ccodeu003E } catch (Exception e) {u003Cu002Fcodeu003Eu003Ccodeu003E u002Fu002F仅仅是重复抛异常 未作任何处理u003Cu002Fcodeu003Eu003Ccodeu003E throw e;u003Cu002Fcodeu003Eu003Ccodeu003E }u003Cu002Fcodeu003Eu003Ccodeu003E}u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003E正例:u003Cu002Fpu003Eu003Cpreu003Eu003Ccodeu003Eu002Fu002F多余异常正例u003Cu002Fcodeu003Eu003Ccodeu003Eprivate static String fileReader(String fileName)throws IOException{u003Cu002Fcodeu003Eu003Cbru003Eu003Ccodeu003E try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {u003Cu002Fcodeu003Eu003Ccodeu003E String line;u003Cu002Fcodeu003Eu003Ccodeu003E StringBuilder builder = new StringBuilder;u003Cu002Fcodeu003Eu003Ccodeu003E while ((line = reader.readLine) != ) {u003Cu002Fcodeu003Eu003Ccodeu003E builder.append(line);u003Cu002Fcodeu003Eu003Ccodeu003E }u003Cu002Fcodeu003Eu003Ccodeu003E return builder.toString;u003Cu002Fcodeu003Eu003Ccodeu003E u002Fu002F删除多余的抛异常,或增加其他处理:u003Cu002Fcodeu003Eu003Ccodeu003E u002F*catch (Exception e) {u003Cu002Fcodeu003Eu003Ccodeu003E return “fileReader exception”;u003Cu002Fcodeu003Eu003Ccodeu003E }*u002Fu003Cu002Fcodeu003Eu003Ccodeu003E }u003Cu002Fcodeu003Eu003Ccodeu003E}u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003Eu003Cstrongu003E十一、字符串转化使用String.valueOf(value) 代替 ” ” + valueu003Cu002Fstrongu003Eu003Cu002Fpu003Eu003Cpu003E把其它对象或类型转化为字符串时,使用String.valueOf(value) 比 “”u003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-2″u003E+vu003Cu002Fiu003Ealue 的效率更高。u003Cu002Fpu003Eu003Cpu003E反例:u003Cu002Fpu003Eu003Cpreu003Eu003Cdivu003Eu003Cpreu003Eu003Ccodeu003Eu002Fu002F把其它对象或类型转化为字符串反例:u003Cu002Fcodeu003Eu003Ccodeu003Eint num = 520;u003Cu002Fcodeu003Eu003Ccodeu003Eu002Fu002F “” + valueu003Cu002Fcodeu003Eu003Ccodeu003EString strLove = “” + num;u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cu002Fdivu003Eu003Cu002Fpreu003Eu003Cpu003E正例:u003Cu002Fpu003Eu003Cpreu003Eu003Cdivu003Eu003Cpreu003Eu003Ccodeu003Eu002Fu002F把其它对象或类型转化为字符串正例:u003Cu002Fcodeu003Eu003Ccodeu003Eint num = 520;u003Cu002Fcodeu003Eu003Ccodeu003Eu002Fu002F String.valueOf 效率更高u003Cu002Fcodeu003Eu003Ccodeu003EString strLove = String.valueOf(num);u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cu002Fdivu003Eu003Cu002Fpreu003Eu003Cpu003Eu003Cstrongu003E十二、避免使用BigDecimal(double)u003Cu002Fstrongu003Eu003Cu002Fpu003Eu003Cpu003EBigDecimal(double) 存在精度损失风险,在精确计算或值比较的场景中可能会导致业务逻辑异常。u003Cu002Fpu003Eu003Cpu003E反例:u003Cu002Fpu003Eu003Cpreu003Eu003Cdivu003Eu003Cpreu003Eu003Ccodeu003Eu002Fu002F BigDecimal 反例u003Cu002Fcodeu003Eu003Ccodeu003EBigDecimal bigDecimal = new BigDecimal(0.11D);u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cu002Fdivu003Eu003Cu002Fpreu003Eu003Cpu003E正例:u003Cu002Fpu003Eu003Cpreu003Eu003Ccodeu003Eu002Fu002F BigDecimal 正例u003Cu002Fcodeu003Eu003Ccodeu003EBigDecimal bigDecimal1 = bigDecimal.valueOf(0.11D);u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003E图1. 失去精度u003Cu002Fpu003Eu003Cimg src=”http:u002Fu002Fp1.pstatp.comu002Flargeu002Fpgc-imageu002FRfnK4bL6Kn9GEa” img_width=”1080″ img_height=”625″ alt=”当我遵循了这 16 条规范写代码,同事只对我说了三个字:666″ inline=”0″u003Eu003Cpu003Eu003Cstrongu003E十三、返回空数组和集合而非 u003Cu002Fstrongu003Eu003Cu002Fpu003Eu003Cpu003E若程序运行返回,需要调用方强制检测,否则就会抛出空指针异常;返回空数组或空集合,有效地避免了调用方因为未检测 而抛出空指针异常的情况,还可以删除调用方检测 的语句使代码更简洁。u003Cu002Fpu003Eu003Cpu003E反例:u003Cu002Fpu003Eu003Cpreu003Eu003Ccodeu003Eu002Fu002F返回 反例u003Cu002Fcodeu003Eu003Ccodeu003Epublic static Result getResults {u003Cu002Fcodeu003Eu003Ccodeu003E return ;u003Cu002Fcodeu003Eu003Ccodeu003E}u003Cu002Fcodeu003Eu003Cbru003Eu003Ccodeu003Epublic static List<Result> getResultList {u003Cu002Fcodeu003Eu003Ccodeu003E return ;u003Cu002Fcodeu003Eu003Ccodeu003E}u003Cu002Fcodeu003Eu003Cbru003Eu003Ccodeu003Epublic static Map<String, Result> getResultMap {u003Cu002Fcodeu003Eu003Ccodeu003E return ;u003Cu002Fcodeu003Eu003Ccodeu003E}u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003E正例:u003Cu002Fpu003Eu003Cpreu003Eu003Ccodeu003Eu002Fu002F返回空数组和空集正例u003Cu002Fcodeu003Eu003Ccodeu003Epublic static Result getResults {u003Cu002Fcodeu003Eu003Ccodeu003E return new Result[0];u003Cu002Fcodeu003Eu003Ccodeu003E}u003Cu002Fcodeu003Eu003Cbru003Eu003Ccodeu003Epublic static List<Result> getResultList {u003Cu002Fcodeu003Eu003Ccodeu003E return Collections.emptyList;u003Cu002Fcodeu003Eu003Ccodeu003E}u003Cu002Fcodeu003Eu003Cbru003Eu003Ccodeu003Epublic static Map<String, Result> getResultMap {u003Cu002Fcodeu003Eu003Ccodeu003E return Collections.emptyMap;u003Cu002Fcodeu003Eu003Ccodeu003E}u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003Eu003Cstrongu003E十四、优先使用常量或确定值调用equals 方法u003Cu002Fstrongu003Eu003Cu002Fpu003Eu003Cpu003E对象的equals 方法容易抛空指针异常,应使用常量或确定有值的对象来调用equals 方法。u003Cu002Fpu003Eu003Cpu003E反例:u003Cu002Fpu003Eu003Cpreu003Eu003Cdivu003Eu003Cpreu003Eu003Ccodeu003E u002Fu002F调用 equals 方法反例u003Cu002Fcodeu003Eu003Ccodeu003Eprivate static boolean fileReader(String fileName)throws IOException{u003Cu002Fcodeu003Eu003Ccodeu003E u002Fu002F 可能抛空指针异常u003Cu002Fcodeu003Eu003Ccodeu003E return fileName.equals(“Charming”);u003Cu002Fcodeu003Eu003Ccodeu003E }u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cu002Fdivu003Eu003Cu002Fpreu003Eu003Cpu003E正例:u003Cu002Fpu003Eu003Cpreu003Eu003Ccodeu003Eu002Fu002F调用 equals 方法正例u003Cu002Fcodeu003Eu003Ccodeu003Eprivate static boolean fileReader(String fileName)throws IOException{u003Cu002Fcodeu003Eu003Cbru003Eu003Ccodeu003E u002Fu002F 使用常量或确定有值的对象来调用 equals 方法u003Cu002Fcodeu003Eu003Ccodeu003E return “Charming”.equals(fileName);u003Cu002Fcodeu003Eu003Ccodeu003E u002Fu002F或使用:java.util.Objects.equals 方法u003Cu002Fcodeu003Eu003Ccodeu003E return Objects.equals(“Charming”,fileName);u003Cu002Fcodeu003Eu003Ccodeu003E }u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003Eu003Cstrongu003E十五、枚举的属性字段必须是私有且不可变u003Cu002Fstrongu003Eu003Cu002Fpu003Eu003Cpu003E枚举通常被当做常量使用,如果枚举中存在公共属性字段或设置字段方法,那么这些枚举常量的属性很容易被修改;理想情况下,枚举中的属性字段是私有的,并在私有构造函数中赋值,没有对应的Setter 方法,最好加上final 修饰符。u003Cu002Fpu003Eu003Cpu003E反例:u003Cu002Fpu003Eu003Cpreu003Eu003Ccodeu003Epublic enum SwitchStatus {u003Cu002Fcodeu003Eu003Ccodeu003E u002Fu002F 枚举的属性字段反例u003Cu002Fcodeu003Eu003Ccodeu003E DISABLED(0, “禁用”),u003Cu002Fcodeu003Eu003Ccodeu003E ENABLED(1, “启用”);u003Cu002Fcodeu003Eu003Cbru003Eu003Ccodeu003E public int value;u003Cu002Fcodeu003Eu003Ccodeu003E private String description;u003Cu002Fcodeu003Eu003Cbru003Eu003Ccodeu003E private SwitchStatus(int value, String description) {u003Cu002Fcodeu003Eu003Ccodeu003E this.value = value;u003Cu002Fcodeu003Eu003Ccodeu003E this.description = description;u003Cu002Fcodeu003Eu003Ccodeu003E }u003Cu002Fcodeu003Eu003Cbru003Eu003Ccodeu003E public String getDescription {u003Cu002Fcodeu003Eu003Ccodeu003E return description;u003Cu002Fcodeu003Eu003Ccodeu003E }u003Cu002Fcodeu003Eu003Cbru003Eu003Ccodeu003E public void setDescription(String description) {u003Cu002Fcodeu003Eu003Ccodeu003E this.description = description;u003Cu002Fcodeu003Eu003Ccodeu003E }u003Cu002Fcodeu003Eu003Ccodeu003E}u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003E正例:u003Cu002Fpu003Eu003Cpreu003Eu003Ccodeu003Epublic enum SwitchStatus {u003Cu002Fcodeu003Eu003Ccodeu003E u002Fu002F 枚举的属性字段正例u003Cu002Fcodeu003Eu003Ccodeu003E DISABLED(0, “禁用”),u003Cu002Fcodeu003Eu003Ccodeu003E ENABLED(1, “启用”);u003Cu002Fcodeu003Eu003Cbru003Eu003Ccodeu003E u002Fu002F final 修饰u003Cu002Fcodeu003Eu003Ccodeu003E private final int value;u003Cu002Fcodeu003Eu003Ccodeu003E private final String description;u003Cu002Fcodeu003Eu003Cbru003Eu003Ccodeu003E private SwitchStatus(int value, String description) {u003Cu002Fcodeu003Eu003Ccodeu003E this.value = value;u003Cu002Fcodeu003Eu003Ccodeu003E this.description = description;u003Cu002Fcodeu003Eu003Ccodeu003E }u003Cu002Fcodeu003Eu003Cbru003Eu003Ccodeu003E u002Fu002F 没有Setter 方法u003Cu002Fcodeu003Eu003Ccodeu003E public int getValue {u003Cu002Fcodeu003Eu003Ccodeu003E return value;u003Cu002Fcodeu003Eu003Ccodeu003E }u003Cu002Fcodeu003Eu003Cbru003Eu003Ccodeu003E public String getDescription {u003Cu002Fcodeu003Eu003Ccodeu003E return description;u003Cu002Fcodeu003Eu003Ccodeu003E }u003Cu002Fcodeu003Eu003Ccodeu003E}u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003Eu003Cstrongu003E十六、tring.split(String regex)部分关键字需要转译u003Cu002Fstrongu003Eu003Cu002Fpu003Eu003Cpu003E使用字符串String 的plit 方法时,传入的分隔字符串是正则表达式,则部分关键字(比如 .[]()\| 等)需要转义。u003Cu002Fpu003Eu003Cpu003E反例:u003Cu002Fpu003Eu003Cpreu003Eu003Cdivu003Eu003Cpreu003Eu003Ccodeu003E u002Fu002F String.split(String regex) 反例u003Cu002Fcodeu003Eu003Ccodeu003EString split = “a.ab.abc”.split(“.”);u003Cu002Fcodeu003Eu003Ccodeu003ESystem.out.println(Arrays.toString(split)); u002Fu002F 结果为u003Cu002Fcodeu003Eu003Ccodeu003EString split1 = “a|ab|abc”.split(“|”);u003Cu002Fcodeu003Eu003Ccodeu003ESystem.out.println(Arrays.toString(split1)); u002Fu002F 结果为[“a”, “|”, “a”, “b”, “|”, “a”, “b”, “c”]u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cu002Fdivu003Eu003Cu002Fpreu003Eu003Cpu003E正例:u003Cu002Fpu003Eu003Cpreu003Eu003Ccodeu003Eu002Fu002F String.split(String regex) 正例u003Cu002Fcodeu003Eu003Ccodeu003Eu002Fu002F . 需要转译u003Cu002Fcodeu003Eu003Ccodeu003EString split2 = “a.ab.abc”.split(“\\.”);u003Cu002Fcodeu003Eu003Ccodeu003ESystem.out.println(Arrays.toString(split2)); u002Fu002F 结果为[“a”, “ab”, “abc”]u003Cu002Fcodeu003Eu003Cbru003Eu003Ccodeu003Eu002Fu002F | 需要转译u003Cu002Fcodeu003Eu003Ccodeu003EString split3 = “a|ab|abc”.split(“\\|”);u003Cu002Fcodeu003Eu003Ccodeu003ESystem.out.println(Arrays.toString(split3)); u002Fu002F 结果为[“a”, “ab”, “abc”]u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003E图2. String.split(String regex) 正反例u003Cu002Fpu003Eu003Cimg src=”http:u002Fu002Fp3.pstatp.comu002Flargeu002Fpgc-imageu002FRfnK4be8IVnbON” img_width=”1080″ img_height=”510″ alt=”当我遵循了这 16 条规范写代码,同事只对我说了三个字:666″ inline=”0″u003E”

原文始发于:当我遵循了这 16 条规范写代码,同事只对我说了三个字:666

主题测试文章,只做测试使用。发布者:逗乐男神i,转转请注明出处:http://www.cxybcw.com/17880.html

联系我们

13687733322

在线咨询:点击这里给我发消息

邮件:1877088071@qq.com

工作时间:周一至周五,9:30-18:30,节假日休息

QR code