原创地址:   http://www.cnblogs.com/Alandre/  (泥沙砖瓦浆木匠),需要转载的,保留下! Thanks

The reasonable man adapts himself to the world;the unreasonable one persists in trying to adapt the world to himself. —萧伯纳

相信自己看得懂就看得懂了,相信自己能写下去,我就开始写了.其实也简单—泥沙砖瓦浆木匠

Written In The Font

Today , I am writing my java notes about <编写高质量代码改善java程序的151个建议> -秦小波.

Three pieces[1-3]:

1.Don't code by confusing letters in the constants and variables  [不要在常量和变量中出现易混淆的字母]

2.Don't change the constants into the variable.                           [莫让常量蜕变成变量]

3.Make the the types of ternary operators the same.                 [三元操作符的类型务必一致]

Don't code by confusing letters in the constants and variables

from the book:

  1. public class Client01 {
  2. public static void main(String [] args)
  3. {
  4. long i = 1l;
  5. System.out.println("i的两倍是:"+(i+i));
  6. }
  7. }

Outputs:

  1. 2  

(unbelieved?just ctrl c + ctrl v run the code !)

In my words:

‘1l’is not ‘11’. But we always code by the confusing things . then bebug for a long time , finally we will laught at ourselves with the computer and codes face to face.”What fucking are the coder?”lol,smile ! carm down , because u r so lucky to read this . i will tell some excemples to keep them away. away ? really away?

Step 1: somthing about Coding Standards


★Constants should always be all-uppercase, with underscores to separatewords. [常量都要大写,用下划线分开]

See a case from my project ITP:

  1. package sedion.jeffli.wmuitp.constant;
  2.  
  3. public class AdminWebConstant
  4. {
  5.  
  6. public static final String ADMIN_BASE = "/admin";
  7. public static final String WEB_BASE = ADMIN_BASE + "/web";
  8.  
  9. /**
  10. * view
  11. */
  12. public static final String ADMIN_LOGIN_VIEW = WEB_BASE + "/login";
  13. public static final String ADMIN_INDEX_VIEW = ADMIN_BASE + "/index/index";
  14.  
  15. /**
  16. * 返回String状态
  17. */
  18. public static final int RESULT_SUCCESS = 1;
  19. public static final int RESULT_FAIL = 0;
  20. }

★Camel Case:[变量命名驼峰原则,自然你也可以选择其他的法则等]

if u wanna do it , right now ! it can make your codes more beautiful and clean! amazing ! u learned it , keep on!!!

  1. package sedion.jeffli.wmuitp.util;
  2.  
  3. import javax.servlet.http.HttpSession;
  4.  
  5. import sedion.jeffli.wmuitp.entity.TeacherInfo;
  6. import sedion.jeffli.wmuitp.entity.UserLogin;
  7.  
  8. public class AdminUtil
  9. {
  10. public static final String ADMIN = "admin";
  11. public static final String ADMIN_ID = "adminID";
  12. public static final String TEACHER_ID = "teacherID";
  13.  
  14. public static void saveAdminToSession(HttpSession session,UserLogin userLogin)
  15. {
  16. session.setAttribute(ADMIN, userLogin);
  17. }
  18.  
  19. public static void saveAdminIDToSession(HttpSession session,UserLogin userLogin)
  20. {
  21. session.setAttribute(ADMIN_ID, userLogin.getUlId().toString());
  22. }
  23. public static UserLogin getUserLoginFromHttpSession(HttpSession session)
  24. {
  25. Object attribute = session.getAttribute(ADMIN);
  26. return attribute == null ? null : (UserLogin)attribute;
  27. }
  28.  
  29. public static String getUserLoginIDFromHttpSession(HttpSession session)
  30. {
  31. Object attribute = session.getAttribute(ADMIN_ID);
  32. return attribute == null ? null : (String)attribute;
  33. }
  34.  
  35. }

#please remeber the camel , then u can write a nice code.

Step 2: somthing can make your program easier to understand


some letters should not be used with the numbers,like  l O … they are the brother of the numbers.but we can do some to avoid. like use ‘L’ , and write some notes about them.

Don't change the constants into the variable

A magical demo:

  1. public class Client02
  2. {
  3. public static void main(String [] args)
  4. {
  5. System.out.println("const can change: "+ Const.RAND_COSNT);
  6. }
  7.  
  8. //接口常量
  1. interface Const
  2. {
  3. public static final int RAND_COSNT = new Random().nextInt();
  4. }
  5. }

#I think the demo is bad. RAND_COSNT is not a constant and we never do that.

what is Constants ?


    Constants are immutable values which are known at compile time and do not change for the life of the program.But if the project is too large to manage.There will be a problem.Let me show u!

example:

  1. //file: A.java
  2. public interface A
  3. {
  4. String goodNumber = "0.618";
  5. }
  6.  
  7. //file: B.java
  8. public class B
  9. {
  10. public static void main(String [] args)
  11. {
  12. System.out.println("Class A's goodNumber = " +A.goodNumber);
  13. }
  14. }

Outputs:

Class A's goodNumber = 0.618

Now we  change A.java –> goodNumber to “0.6180339887”

  1. //file: A.java
  2. public interface A
  3. {
  4. String goodNumber = "0.6180339887";
  5. }

“javac A.java”then “java B” , we will find the outputs is the same:

Class A's goodNumber = 0.618

why??????????????????

just see the class of B, use “ javap –c B”:

  1. Compiled from "B.java"
  2. public class B {
  3. public B();
  4. Code:
  5. 0: aload_0
  6. 1: invokespecial #1 // Method java/lang/Object."<init>":()V
  7. 4: return
  8.  
  9. public static void main(java.lang.String[]);
  10. Code:
  11. 0: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
  12. 3: ldc #3 // String Class A's goodNumber = 0.618
  13. 5: invokevirtual #4 // Method java/io/PrintStream.printl
  14. n:(Ljava/lang/String;)V
  15. 8: return
  16. }

#3: ldc #3 // String Class A's goodNumber = 0.618

ok , we see! the last interface A was already in the class B. so we can “javac B.java”to deal.

All in all ,

Java Interface is usually the best place to store The Constants.

[Java Interface 通常是常量存放的最佳地点]

A best way store constants : static fianl  XXX    static Object getXXX()


it shows the Java dynamic advantage and a constant principle.

  1. //file:A.java
  2. public class A
  3. {
  4. private static final String goodNumber = "0.6180339887";
  5. public static String getGoodNumber()
  6. {
  7. return goodNumber;
  8. }
  9. }
  10.  
  11. //file:B.java
  12. public class B
  13. {
  14. public static void main(String [] args)
  15. {
  16. System.out.println("Class A's goodNumber = " +A.getGoodNumber());
  17. }
  18. }
  1.  
  1.  

Make the the types of ternary operators the same.

  1. from the book:
  1. public class Client03 {
  2. public static void main(String[] args) {
  3. int i = 80;
  4. String s = String.valueOf(i<100?90:100);
  5. String s1 = String.valueOf(i<100?90:100.0);
  6. System.out.println(" 两者是否相等:"+s.equals(s1));
  7. }
  8. }
  1.  
  1. Outputs:
  1. false

  

see the compiled code ,use “javap –c Client03”,we will see:

  1. 23: if_icmpge 32
  2. 26: ldc2_w #3 // double 90.0d
  3. 29: goto 35
  4. 32: ldc2_w #5 // double 100.0d

 

the transformation rules about ternary operators.


三元操作符类型的转换规则:

1.若两个操作数不可转换,则不做转换,返回值为Object 类型。

2.若两个操作数是明确类型的表达式(比如变量),则按照正常的二进制数字来转换,int 类型转换为long 类型,long 类型转换为float 类型等。

3.若两个操作数中有一个是数字S,另外一个是表达式,且其类型标示为T,那么,若数字S 在T 的范围内,则转换为T 类型;若S 超出了T 类型的范围,则T 转换为S类型(可以参考“建议22”,会对该问题进行展开描述)。

4.若两个操作数都是直接量数字(Literal) ,则返回值类型为范围较大者。

Editor's Note

合抱之木,生于毫末;九层之台,起于累土;千里之行,始于足下. ---老子<<道德经>>

快来加入群【编程乐园】(365234583)。 http://t.cn/RvGAuJr

编写高质量代码改善java程序的151个建议——[1-3]基础?亦是基础的更多相关文章

  1. 博友的 编写高质量代码 改善java程序的151个建议

    编写高质量代码 改善java程序的151个建议 http://www.cnblogs.com/selene/category/876189.html

  2. 编写高质量代码改善java程序的151个建议——导航开篇

    2014-05-16 09:08 by Jeff Li 前言 系列文章:[传送门] 下个星期度过这几天的奋战,会抓紧java的进阶学习.听过一句话,大哥说过,你一个月前的代码去看下,慘不忍睹是吧.确实 ...

  3. 编写高质量代码:改善Java程序的151个建议 --[117~128]

    编写高质量代码:改善Java程序的151个建议 --[117~128] Thread 不推荐覆写start方法 先看下Thread源码: public synchronized void start( ...

  4. 编写高质量代码:改善Java程序的151个建议 --[106~117]

    编写高质量代码:改善Java程序的151个建议 --[106~117] 动态代理可以使代理模式更加灵活 interface Subject { // 定义一个方法 public void reques ...

  5. 编写高质量代码:改善Java程序的151个建议 --[78~92]

    编写高质量代码:改善Java程序的151个建议 --[78~92] HashMap中的hashCode应避免冲突 多线程使用Vector或HashTable Vector是ArrayList的多线程版 ...

  6. 编写高质量代码:改善Java程序的151个建议 --[65~78]

    编写高质量代码:改善Java程序的151个建议 --[65~78] 原始类型数组不能作为asList的输入参数,否则会引起程序逻辑混乱. public class Client65 { public ...

  7. 编写高质量代码:改善Java程序的151个建议 --[52~64]

    编写高质量代码:改善Java程序的151个建议 --[52~64] 推荐使用String直接量赋值 Java为了避免在一个系统中大量产生String对象(为什么会大量产生,因为String字符串是程序 ...

  8. 编写高质量代码:改善Java程序的151个建议 --[36~51]

    编写高质量代码:改善Java程序的151个建议 --[36~51] 工具类不可实例化 工具类的方法和属性都是静态的,不需要生成实例即可访 问,而且JDK也做了很好的处理,由于不希望被初始化,于是就设置 ...

  9. Github即将破百万的PDF:编写高质量代码改善JAVA程序的151个建议

    在通往"Java技术殿堂"的路上,本书将为你指点迷津!内容全部由Java编码的最佳 实践组成,从语法.程序设计和架构.工具和框架.编码风格和编程思想等五大方面,对 Java程序员遇 ...

随机推荐

  1. C++动态库的几点认识

    1.动态库也有lib文件,称为导入库,一般大小只有几k: 2.动态库有静态调用和动态调用两种方式: 静态调用:使用.h和.lib文件 动态调用: 先LoadLibrary,再GetProcAddres ...

  2. version control

    what 版本控制最主要的功能就是追踪文件的变更.它将什么时候.什么人更改了文件的什么内容等信息忠实地了已录下来.每一次文件的改变,文件的版本号都将增加.除了记录版本变更外,版本控制的另一个重要功能是 ...

  3. CentOS 7 rpm安装jdk

    RPM 安装jdk1.8.0_111 ,查询系统自带的jdk rpm -qa | grep java 查询结果如下: [root@bogon ~]# rpm -qa | grep java javap ...

  4. NIOS II 之串口学习

    UART中有6个寄存器分别为control, status, rxdata, txdata, divisor,endofpacket. 的寄存器是16位位宽的. UART会产生一个高电平的中断,当接收 ...

  5. windows下Apache配置多个站点

    1. httpd.conf 找到以下两行去掉注释: # Include conf/extra/httpd-vhosts.conf # LoadModule vhost_alias_module mod ...

  6. prime distance on a tree(点分治+fft)

    最裸的点分治+fft,调了好久,太菜了.... #include<iostream> #include<cstring> #include<cstdio> #inc ...

  7. python3+redis问题求解

    学生管理系统   更新学生信息没做出来,找个大神补全下.谢谢. # 记录: # bug:操作后若退出需要两次退出才行. 待修复 # 下一步:链接redis进行使用. # 更新学生库信息 待完成 imp ...

  8. 【笔记】css基于box的一行时垂直方向居中,多行平均居中,多出部分还省略号代替

    题目很长,其实他就是这样的: 看标题,一行的时候是这样的,在行中间 标题文字多的时候是这样的,变成2行,超出部分用省略号: 但是为了更好的兼容性,没有使用flex,使用的是box布局. 核心代码就是这 ...

  9. 录音--获取语音流(pyAudio)

    这是学习时的笔记,包含相关资料链接,有的当时没有细看,记录下来在需要的时候回顾. 有些较混乱的部分,后续会再更新. 欢迎感兴趣的小伙伴一起讨论,跪求大神指点~ 录音-语音流(pyAudio) tags ...

  10. 兆芯 服务器 win2012/win7装机总结

    兆芯cpu 服务器 win2012/win7装机总结 一.设置U盘启动装机 启动后,esc进入bios修改下图两个地方,都要改,然后保存. 二.重启计算机,进入win安装界面,会出现无法安装,原因是: ...