佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 3782|回复: 34

Effective Java - General Programming

  [复制链接]
发表于 31-12-2010 04:06 PM | 显示全部楼层 |阅读模式
<Effective Java> 是出自于 Joshua Bloch 的一本Java 科技书. 2001 出首版.
至今, 它乃为Java 学者的一本 bible.
该书有9章, 共57个重点.. (除了第一章介绍)

本人目前在拜读这本书..
在此跟大家分享General Programming 一章 里的每一重点..
在此也欢迎大家来讨论.. 日后如果有时间的话, 也会推出另外8章..

29. Minimize the scope of local variables
30. know and use the libraries
31. avoid float and double if exact answers are required
32. Avoid strings where other types are more appropriate
33. Beware the performance of string concatenation
34. Refer to objects by their interfaces
35. Prefer interfaces to reflection
36. Use native methods judiciously
37. Optimize judiciously
38. Adhere to generally accepted naming conventions
回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 31-12-2010 04:19 PM | 显示全部楼层
本帖最后由 jasonmun 于 31-12-2010 05:40 PM 编辑

29. Minimize the scope of local variables

- The most powerful technique for minimize the scope of a local variable is
to declare it where it is first used
(local variable 最好用时才来宣告, 以缩小使用范围)

- Nearly every local variable declaration should contain an initializer.
(几乎每一variable 应该作 initializer)

example :
for (int i=0, n=lst.size(); i<n; i++) // good
for (int i=0; i<lst.size(); i++) // normal, but not the best one

* 这看起来没什么变化, 只是做同样的东西, 但出来的performance可大不同..
回复

使用道具 举报

 楼主| 发表于 31-12-2010 04:27 PM | 显示全部楼层
本帖最后由 jasonmun 于 31-12-2010 05:38 PM 编辑

30. know and use the libraries

- By using a standard library,
you take advantage of the knowledge of the experts who wrote it and
the experience of those who used it before you.
(作者建议大家使用library, 因它是专家们的结晶)

31. avoid float and double if exact answers are required

如果要较正确的答案, 避免用float/double..
如货币的计算, 可以用BigDecimal..
回复

使用道具 举报

 楼主| 发表于 31-12-2010 04:35 PM | 显示全部楼层
本帖最后由 jasonmun 于 31-12-2010 05:36 PM 编辑

32. Avoid strings where other types are more appropriate

String 不适合代替其他type的数据.. 如用不好, 会影响 performance..

33. Beware the performance of string concatenation

小心使用 + operator, 因String是不可变的, 一旦intializer后.
鼓励用StringBuffer.. 它不象String, 从头到尾只会产生一个object.

* 这点在JAVA 书普遍上都有提到..
回复

使用道具 举报

 楼主| 发表于 31-12-2010 04:48 PM | 显示全部楼层
本帖最后由 jasonmun 于 31-12-2010 05:35 PM 编辑

34. Refer to objects by their interfaces

优先依赖于 interface, 而不是用class 来引用object

35. Prefer interfaces to reflection

interface 优先使用.. 因使用 reflection 要牺牲掉performance (速度慢一倍)..
回复

使用道具 举报

 楼主| 发表于 31-12-2010 05:06 PM | 显示全部楼层
本帖最后由 jasonmun 于 31-12-2010 05:34 PM 编辑

36. Use native methods judiciously

JNI 允许JAVA 调用 native method (比如用C写的)
很少情况需要用它来提高性能, 如果必须用它, 也尽可能少用.
因只要程序有BUG, 会破坏掉整个应用程序.

37. Optimize judiciously

谨慎优化, 因优化不好, 反而让系统变慢

38. Adhere to generally accepted naming conventions

根据惯例来命名..
回复

使用道具 举报

Follow Us
发表于 31-12-2010 11:37 PM | 显示全部楼层
可以提升为精华吗 ?
回复

使用道具 举报

发表于 1-1-2011 01:39 AM | 显示全部楼层
example :
for (int i=0, n=lst.size(); i<n; i++) // good
for (int i=0; i<lst.size(); i++) // normal, but not the best one

不是很明白这个的分别
回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 1-1-2011 02:07 AM | 显示全部楼层
回复 8# mdw1121686

读一个primitive data, 还是一直去loop读一个method的return value为快呢?
想一想都应该明白..
回复

使用道具 举报

发表于 1-1-2011 02:14 AM | 显示全部楼层
回复  mdw1121686

读一个primitive data, 还是一直去loop读一个method的return value为快呢?
想一想都 ...
jasonmun 发表于 1-1-2011 02:07 AM



哦哦。。原来! 谢谢!
回复

使用道具 举报

发表于 2-1-2011 12:23 PM | 显示全部楼层
优先依赖于 interface, 而不是用class 来引用object

为什么
教程的老外很喜欢酱。
可是还是很不明白当中好处。
回复

使用道具 举报

发表于 2-1-2011 01:13 PM | 显示全部楼层
优先依赖于 interface, 而不是用class 来引用object

为什么
教程的老外很喜欢酱。
可是还是很不 ...
宅男-兜着走 发表于 2-1-2011 12:23 PM


我看过看过类似的 CODE , 我以前 SENIOR 就是这样的写法 。
现在要 MAINTAIN 他的 CODE , 真的有点难吃消 。
回复

使用道具 举报

 楼主| 发表于 2-1-2011 05:06 PM | 显示全部楼层
优先依赖于 interface, 而不是用class 来引用object

为什么
教程的老外很喜欢酱。
可是还是很不 ...
宅男-兜着走 发表于 2-1-2011 12:23 PM


interface 有利于设计.. 给了程序扩展性..
如果直接用class, 会造成程序非直接依赖所写的class不可.
回复

使用道具 举报

发表于 4-1-2011 01:01 PM | 显示全部楼层
建议申精
有什么非看不可的书吗?
回复

使用道具 举报

发表于 4-1-2011 01:17 PM | 显示全部楼层
30. know and use the libraries

- By using a standard library,
you take advantage of the knowledg ...
jasonmun 发表于 31-12-2010 04:27 PM


11 Lesser Known 3rd Party Libraries For Every Project

http://codedependents.com/2009/10/26/11-lesser-known-3rd-party-libraries-for-every-project/
回复

使用道具 举报

发表于 23-1-2011 11:44 PM | 显示全部楼层
可是很多时候老板只在乎output,会work吗?
后面的backend 品质,他不得空鸟...
回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 24-1-2011 03:10 AM | 显示全部楼层
本帖最后由 jasonmun 于 24-1-2011 01:16 PM 编辑
可是很多时候老板只在乎output,会work吗?
后面的backend 品质,他不得空鸟...
User. 发表于 23-1-2011 11:44 PM


伯乐(老板)需要千里马(懂的有效编程的程序员)..
如果伯乐真的懂马的话, 他懂如何去找所要的马..
如果不懂的话, 也许一时半刻对他没实际的影响..

但长久后, 这到底有没影响.. 想想大家都应该知道..
(要知道开发系统可不是一天两天的事.
如果系统因长年不当的写法,导致越跑越慢,这要怪谁呢?)
回复

使用道具 举报

发表于 24-1-2011 02:45 PM | 显示全部楼层
伯乐(老板)需要千里马(懂的有效编程的程序员)..
如果伯乐真的懂马的话, 他懂如何去找所要的马..
如果 ...
jasonmun 发表于 24-1-2011 03:10 AM


USER 会 COMPLAIN 的 , 如果走得很慢
回复

使用道具 举报

发表于 25-1-2011 12:40 AM | 显示全部楼层
回复 18# 兔仙人
有时候有complain才证明有存在的价值,一次做好反而没人记得。。
回复

使用道具 举报

发表于 5-2-2011 10:45 PM | 显示全部楼层
伯乐(老板)需要千里马(懂的有效编程的程序员)..
如果伯乐真的懂马的话, 他懂如何去找所要的马..
如果 ...
jasonmun 发表于 24-1-2011 03:10 AM

伯乐希望马儿好(code quality),也希望马儿不吃草(code得快),大大有什么对策呢?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 4-11-2025 09:08 PM , Processed in 0.136967 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表