The reasonable man adapts himself to the world;the unreasonable one persists in trying to adapt the world to himself. —蕭伯納java
相信本身看得懂就看得懂了,相信本身能寫下去,我就開始寫了.其實也簡單—泥沙磚瓦漿木匠web
Today , I am writing my java notes about <編寫高質量代碼改善java程序的151個建議> -秦小波.編程
Three pieces[1-3]:session
1.Don't code by confusing letters in the constants and variables [不要在常量和變量中出現易混淆的字母]dom
2.Don't change the constants into the variable. [莫讓常量蛻變成變量]post
3.Make the the types of ternary operators the same. [三元操做符的類型務必一致]ui
from the book:this
public class Client01 { public static void main(String [] args) { long i = 1l; System.out.println("i的兩倍是:"+(i+i)); } }
Outputs:spa
1
|
2
|
(unbelieved?just ctrl c + ctrl v run the code !)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.
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:
|
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:
|
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."<init>":()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 ,
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()); } }
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) ,則返回值類型爲範圍較大者。
合抱之木,生於毫末;九層之臺,起於累土;千里之行,始於足下. ---老子<<道德經>>
快來加入羣【編程樂園】(365234583)。 http://t.cn/RvGAuJr