博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
编写高质量代码改善java程序的151个建议——[1-3]基础?亦是基础
阅读量:5826 次
发布时间:2019-06-18

本文共 6744 字,大约阅读时间需要 22 分钟。

hot3.png

    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:

复制代码

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

复制代码

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:

复制代码

package sedion.jeffli.wmuitp.constant;public class AdminWebConstant {        public static final String ADMIN_BASE     = "/admin";    public static final String WEB_BASE       = ADMIN_BASE + "/web";    /**     * view     */    public static final String ADMIN_LOGIN_VIEW = WEB_BASE   + "/login";    public static final String ADMIN_INDEX_VIEW = ADMIN_BASE + "/index/index";        /**     * 返回String状态     */    public static final int RESULT_SUCCESS = 1;    public static final int RESULT_FAIL    = 0;}

复制代码

  

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

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

复制代码

package sedion.jeffli.wmuitp.util;import javax.servlet.http.HttpSession;import sedion.jeffli.wmuitp.entity.TeacherInfo;import sedion.jeffli.wmuitp.entity.UserLogin;public class AdminUtil{    public static final String ADMIN        = "admin";    public static final String ADMIN_ID     = "adminID";    public static final String TEACHER_ID   = "teacherID";        public static void saveAdminToSession(HttpSession session,UserLogin userLogin)    {        session.setAttribute(ADMIN, userLogin);    }        public static void saveAdminIDToSession(HttpSession session,UserLogin userLogin)    {        session.setAttribute(ADMIN_ID, userLogin.getUlId().toString());    }    public static UserLogin getUserLoginFromHttpSession(HttpSession session)    {        Object attribute = session.getAttribute(ADMIN);        return attribute == null ? null : (UserLogin)attribute;    }        public static String getUserLoginIDFromHttpSession(HttpSession session)    {        Object attribute = session.getAttribute(ADMIN_ID);        return attribute == null ? null : (String)attribute;    }    }

复制代码

#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:

复制代码

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

复制代码

#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:

复制代码

//file: A.javapublic interface A{    String goodNumber = "0.618";}//file: B.javapublic class B{    public static void main(String [] args)    {        System.out.println("Class A's goodNumber = " +A.goodNumber);    }        }

复制代码

 

Outputs:

Class A's goodNumber = 0.618

 

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

//file: A.javapublic interface A{    String goodNumber = "0.6180339887";}

 

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”:

复制代码

Compiled from "B.java"public class B {  public B();    Code:       0: aload_0       1: invokespecial #1                  // Method java/lang/Object."
":()V       4: return  public static void main(java.lang.String[]);    Code:       0: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;       3: ldc           #3                  // String Class A's goodNumber = 0.618       5: invokevirtual #4                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V       8: return}

复制代码

#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.

复制代码

//file:A.javapublic class A{    private static final String goodNumber = "0.6180339887";    public static String getGoodNumber()    {        return goodNumber;    }}//file:B.javapublic class B{    public static void main(String [] args)    {        System.out.println("Class A's goodNumber = " +A.getGoodNumber());    }        }

复制代码

 
 

Make the the types of ternary operators the same.

from the book:

复制代码

public class Client03 {    public static void main(String[] args) {        int i = 80;        String s  = String.valueOf(i<100?90:100);        String s1 = String.valueOf(i<100?90:100.0);        System.out.println(" 两者是否相等:"+s.equals(s1));    }}

复制代码

 
Outputs:
1
false

  

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

      23: if_icmpge     32      26: ldc2_w        #3                  // double 90.0d      29: goto          35      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)。 

转载于:https://my.oschina.net/jeffli1993/blog/269860

你可能感兴趣的文章
倒计时:计算时间差
查看>>
Linux/windows P2V VMWare ESXi
查看>>
Windows XP倒计时到底意味着什么?
查看>>
tomcat一步步实现反向代理、负载均衡、内存复制
查看>>
运维工程师在干什么学些什么?【致菜鸟】
查看>>
Linux中iptables详解
查看>>
java中回调函数以及关于包装类的Demo
查看>>
maven异常:missing artifact jdk.tools:jar:1.6
查看>>
终端安全求生指南(五)-——日志管理
查看>>
Nginx 使用 openssl 的自签名证书
查看>>
创业维艰、守成不易
查看>>
PHP环境安装套件:快速安装LAMP环境
查看>>
CSS3
查看>>
ul下的li浮动,如何是ul有li的高度
查看>>
C++ primer plus
查看>>
python mysqlDB
查看>>
UVALive 3942 Remember the Word Tire+DP
查看>>
从微软的DBML文件中我们能学到什么(它告诉了我们什么是微软的重中之重)~目录...
查看>>
被需求搞的一塌糊涂,怎么办?
查看>>
c_数据结构_队的实现
查看>>