编写高质量代码改善java程序的151个建议——[1-3]基础?亦是基础
原创地址: 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:
public class Client01 {
public static void main(String [] args)
{
long i = 1l;
System.out.println("i的两倍是:"+(i+i));
}
}
Outputs:
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.java
public interface A
{
String goodNumber = "0.618";
} //file: B.java
public 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.java
public 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.printl
n:(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.java
public class A
{
private static final String goodNumber = "0.6180339887";
public static String getGoodNumber()
{
return goodNumber;
}
} //file:B.java
public 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:
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)。 http://t.cn/RvGAuJr
编写高质量代码改善java程序的151个建议——[1-3]基础?亦是基础的更多相关文章
- 博友的 编写高质量代码 改善java程序的151个建议
编写高质量代码 改善java程序的151个建议 http://www.cnblogs.com/selene/category/876189.html
- 编写高质量代码改善java程序的151个建议——导航开篇
2014-05-16 09:08 by Jeff Li 前言 系列文章:[传送门] 下个星期度过这几天的奋战,会抓紧java的进阶学习.听过一句话,大哥说过,你一个月前的代码去看下,慘不忍睹是吧.确实 ...
- 编写高质量代码:改善Java程序的151个建议 --[117~128]
编写高质量代码:改善Java程序的151个建议 --[117~128] Thread 不推荐覆写start方法 先看下Thread源码: public synchronized void start( ...
- 编写高质量代码:改善Java程序的151个建议 --[106~117]
编写高质量代码:改善Java程序的151个建议 --[106~117] 动态代理可以使代理模式更加灵活 interface Subject { // 定义一个方法 public void reques ...
- 编写高质量代码:改善Java程序的151个建议 --[78~92]
编写高质量代码:改善Java程序的151个建议 --[78~92] HashMap中的hashCode应避免冲突 多线程使用Vector或HashTable Vector是ArrayList的多线程版 ...
- 编写高质量代码:改善Java程序的151个建议 --[65~78]
编写高质量代码:改善Java程序的151个建议 --[65~78] 原始类型数组不能作为asList的输入参数,否则会引起程序逻辑混乱. public class Client65 { public ...
- 编写高质量代码:改善Java程序的151个建议 --[52~64]
编写高质量代码:改善Java程序的151个建议 --[52~64] 推荐使用String直接量赋值 Java为了避免在一个系统中大量产生String对象(为什么会大量产生,因为String字符串是程序 ...
- 编写高质量代码:改善Java程序的151个建议 --[36~51]
编写高质量代码:改善Java程序的151个建议 --[36~51] 工具类不可实例化 工具类的方法和属性都是静态的,不需要生成实例即可访 问,而且JDK也做了很好的处理,由于不希望被初始化,于是就设置 ...
- Github即将破百万的PDF:编写高质量代码改善JAVA程序的151个建议
在通往"Java技术殿堂"的路上,本书将为你指点迷津!内容全部由Java编码的最佳 实践组成,从语法.程序设计和架构.工具和框架.编码风格和编程思想等五大方面,对 Java程序员遇 ...
随机推荐
- thinkpadE系列重装系统:u盘启动
一.下载深度装机大师,制作启动u盘. 二.重启电脑:按F1;进入bios设置: thinkpad e430c笔记本使用u盘装系统时无法使用u盘启动,这是由于thinkpad e430c笔记本u ...
- 开发HR人事考试系统介绍
确定好需要开发的模块以及功能 一套人事考试系统主要模块: 1)组织管理:公司部门成员信息 2)人事管理:人事信息,离职管理,职务管理,岗位管理: 3)考勤管理:班次设置,停工放假,假日设置,刷卡记录, ...
- Vue 获取登录用户名
本来是打算登录的时候把用户名传过去,试了几次都没成功,然后改成用cookie保存用户名,然后在读取就行了, 登录时候设置cookie setCookie(c_name,c_pwd,exdays) { ...
- 计算机爱好者协会技术贴markdown第二期
上一期我们学了多级标题,加粗,加斜以及蛮好看的小方块,这一期来继续学习吧 Txt版本: *上一期说这样可以加斜* _其实这样也可以加斜_ **上一期说这样可以加粗** __其实这样也可以加粗__ ** ...
- sql语句创建数据表
unsigned 数值类型 无符号化 AUTO_INCREMENT 自增长 PRIMARY KEY 唯一主键 COMMENT 备注信息 BIT 类型只有1和0两个值 enum 枚举数值类型 UNIQU ...
- Java集合不能存放基本数据类型
Java集合不能存放基本数据类型,只能存放对象的引用. 每个集合元素都是一个引用变量,实际内容都存放在堆内或方法区里面, 但是基本数据类型是在栈内存上分配空间的,栈上的数据随时会被收回. 如何解决? ...
- restful状态码常用
在进行后端接口API封装的过程中,需要考虑各种错误信息的输出.一般情况下,根据相应问题输出适合的HTTP状态码,可以方便前端快速定位错误,减少沟通成本. HTTP状态码有很多,每个都有对应的含义,下面 ...
- JavaScript基础视频教程总结(131-140章)
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- gulp使用详解
前面整理了 gulp使用入门,本节介绍相关的压缩 1.压缩js文件 步骤同基础步骤,相关可以看入门篇 npm install gulp-uglify --save-dev 引入js压缩库 gulpfi ...
- 消除blur属性的边框
直接设置样式为: outline:none <!DOCTYPE html> <html lang="en"> <head> <meta ...