惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Cloudbric
Cloudbric
E
Exploit-DB.com RSS Feed
SecWiki News
SecWiki News
Forbes - Security
Forbes - Security
N
News | PayPal Newsroom
S
Security @ Cisco Blogs
Schneier on Security
Schneier on Security
V
V2EX - 技术
S
Secure Thoughts
W
WeLiveSecurity
Google DeepMind News
Google DeepMind News
C
CERT Recently Published Vulnerability Notes
NISL@THU
NISL@THU
S
Securelist
S
Security Archives - TechRepublic
Know Your Adversary
Know Your Adversary
V
Vulnerabilities – Threatpost
Security Latest
Security Latest
Recent Commits to openclaw:main
Recent Commits to openclaw:main
G
GRAHAM CLULEY
H
Hacker News: Front Page
Microsoft Azure Blog
Microsoft Azure Blog
I
Intezer
Google Online Security Blog
Google Online Security Blog
美团技术团队
阮一峰的网络日志
阮一峰的网络日志
T
The Exploit Database - CXSecurity.com
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Webroot Blog
Webroot Blog
Jina AI
Jina AI
Engineering at Meta
Engineering at Meta
P
Proofpoint News Feed
The Cloudflare Blog
I
InfoQ
L
LangChain Blog
U
Unit 42
P
Proofpoint News Feed
S
Schneier on Security
S
Security Affairs
Y
Y Combinator Blog
T
Tenable Blog
N
News and Events Feed by Topic
MyScale Blog
MyScale Blog
量子位
Google DeepMind News
Google DeepMind News
Cyberwarzone
Cyberwarzone
博客园 - 聂微东
D
Darknet – Hacking Tools, Hacker News & Cyber Security
GbyAI
GbyAI
AWS News Blog
AWS News Blog

博客园 - 丁保国

单点登录系统(SSO)详细设计说明书(上篇) ALBPM Service Config [转] Markdown 皓首穷经还是及时行乐! 有用的iOS网站地址 [股票] 入市 Record[Loop]_To_Element Get Started with EC2 准备工作 写在黎明的前夕 - 丁保国 我希望有一套自己的房子 第15章 泛型 第11章 持有对象 第17章 容器深入研究 断点续传的原理 - 丁保国 linux中waitpid系统调用 在Linux系统环境下安装配置JDK常用方法 感冒 《 Linux的安装和入门 》
《编写高质量代码》读书笔记一
丁保国 · 2019-08-22 · via 博客园 - 丁保国

《编写高质量代码》读书笔记一
这些建议,我们读过之后,觉得很有必要来分享一下,希望可以帮到你。
少读书,多思考,提高对程序本身的认识。
建议 1: 不要在常量和变量中出现容易混淆的字母
public class Client {
    public static void main(String[] args)} {
        long i = 1l;
        System.out.println("Double i is: " + (i+i));
    }
}

实际结果:2
--------------------------------------
注意:字母“l”作为长整型标志时务必大写。
--------------------------------------

建议 2: 莫让常量蜕变成变量
public class Client {
    public void mani (String[] args) {
        System.out.println("Const is: " + Const.RAND_CONST);
    }
}

interface Const {
    public static final int RAND_CONST = new Random().nextInt();
}

--------------------------------------
注意:务必让常量的值在运行期保持不变。
--------------------------------------

建议 3: 三元操作符的类型必一致
public class Client {
    int i = 80;
    String s = String.valueOf(i<100?90:100);
    String s1 = String.valueOf(i<100?90:100.0);
    System.out.println("Both equal: "+s.equals(s1));
}

实际结果:false
String s1 = String.valueOf(i<100?90:100.0);// 100.0是浮点型

建议 4: 避免带有变长参数的方法重载
public class Client {
    //Method 1
    public void calPrice(int price, int discount) {
        //TODO
    }
    //Method 2
    public void calPrice(int price, int... discounts) {
        //TODO
    }
    public static void main(String[] args) {
        Client client = new Client();
        client.calPrice(49900, 75);
    }
}

哪个方法会被call? 为什么?
来问我啊,哈哈哈哈

建议 5: 别让null值和空值威胁到变长方法
public class Client {
    
}

建议 6: 覆写变长方法也循规蹈矩

建议 7: 警惕自增的陷阱

建议 8: 不要让旧语法困扰你

建议 9: 少用静态导入

建议 10: 不要在本类中覆盖静态导入的变量和方法

建议 11: 养成良好习惯,显式声明UID

建议 12: 避免用序列化类在构造函数中为不变量赋值

建议 13: 避免为final变量复杂赋值

建议 14: 使用序列化类的私有方法巧妙解决部分属性持久化问题

建议 15: break万万不可忘

建议 16: 易变业务使用脚本语言编写

建议 17: 慎用动态编译

建议 18: 避免instanceof非预期结果

建议 19: 断言绝对不是鸡肋

建议 20: 不要只替换一个类