Chapt8 General Programming
45.最小化局部变量的作用域
>第一次使用前声明
>几乎所有的局部变量都应该在声明时包含一个初始值
例外:在try块外声明,在块内和块外使用
>使用for循环优于while循环
>保持方法短小、专一
46.for-each循环优于传统的for循环
>例外情况:需要针对特定索引的元素操作,或者需要替换或者删除元素
47.了解并使用类库
>所有的java程序员都应该熟悉java.lang, java.util包的内容,java.io作为扩展
特别提到了Collections Framework,java.util.concurrent
48.需要精确的答案时避免使用float和double
>尤其是涉及到金钱计算
49.内置类型优于包装类型
>==比较,内置类型比较值,包装类型比较引用,使用==号比较包装类型几乎都是错误的
>当内置类型和包装类型在一个操作中出现时,内置类型会自动装箱为包装类型
当操作的数量级比较大时,自动装箱会创建很多的临时对象,造成性能损失
>包装类型的默认值为null,不同于内置对象的默认值
>自动装箱简化了使用包装类型的代码冗余,但是并没有减少危险
当包装类型为null时,用==比较会报空指针;自动解箱也会报空指针,如int>Integer
50.当其它类型更合适时,避免使用String
>String被设计用来表示文本
>String不适合代替其它类型
不适合代替枚举类型
不适合代替聚合类型
不适合代替功能表(capabilities)
51.了解字符串连接的性能
>重复的使用字符串连接操作来连接n个字符串的时间复杂度是n的平方
>当拼接操作数量很大时,为了获取可接受的性能,使用StringBuilder来代替String
52.通过接口来引用对象
>如果存在合适的接口,那么参数、返回值、变量和字段都应该用接口来声明
>如果你习惯了使用接口来作为类型,你的程序将会灵活得多
>如果没有合适的接口,使用类而不是接口来引用对象是完全合适的
value classes String/BigInteger 或者没有接口,如Random
class-based framework 使用base class引用
类提供了接口没有的额外功能
53.接口优于反射
>使用反射的代价
失去了编译期间类型检查的益处
通过反射实现的代码笨拙且冗长
性能很糟糕
>反射通常只用在设计时,如框架、工具、RPC、代码分析工具等
>通常,普通应用在运行时不应该通过反射来访问对象
有时通过反射创建对象,但访问对象时还是通过接口或方法
54.谨慎地使用本地方法
>本地方法用于访问历史代码,或者为了提高关键部分的性能
>建议不要通过使用本地方法来提高性能
现代的类库实现已经比较好,虚拟机的性能也很好
本地方法不安全,可读性很差,也很难写
如果确实需要使用本地方法,使用尽可能少的本地方法代码,详细测试
55.谨慎地优化
>格言
不成熟的优化是很多错误的根源
>努力写好的程序,而不是快的程序
好的通常都比较快
>努力避免那些会限制性能的设计决定
>考虑你的API设计决定会导致的性能影响
>为了获取好的性能而曲改API是非常糟糕的决定
56.遵守通用的命名规范
相关推荐
Python is a wide used general, high-level programming language. Its style philosophy emphasizes code readability, and its syntax allows programmers to precise ideas in fewer lines of code that might ...
C is a general-purpose, procedural computer programming language developed in the early 1970s by Dennis Ritchie at Bell Labs. It is one of the most widely used programming languages and serves as the ...
Effective Java Best selling guide about best programming practices. Java Documentation in HTMLHelp and WinHelp Formats For Windows users. Code Conventions for the Java Programming Language ...
Assuming an intermediate level of MATLAB programming knowledge, the book not only concentrates on MATLAB coding techniques but also discusses topics critical to general software development....
I hope that by the end of the book you’ll have a much better idea of what the design of good programs is like, what makes an effective and productive developer, and how to develop larger pieces of ...
- **Programming Models**: Using Java, Pig, Perl, and Hive for implementing MapReduce jobs. - **Performance Tuning**: Techniques for optimizing MapReduce jobs to improve performance. #### Chapter 5: ...
Another useful rule of thumb: it's typically not cost effective to inline functions with loops or switch statements (unless, in the common case, the loop or switch statement is never executed)....