Effective Java 45 Minimize the scope of local variables
Principle
- The most powerful technique for minimizing the scope of a local variable is to declare it where it is first used.
- Nearly every local variable declaration should contain an initializer.
Note
If a variable is initialized by a method that throws a checked exception, it must be initialized inside a try block. If the value must be used outside of the try block, then it must be declared before the try block.
- prefer for loops to while loops
Advantage
- The loops are completely independent, so there's no harm in reusing the element (or iterator) variable name.
- It's much less likely that you'll make the cut-and-paste error, as there's no incentive to use different variable names in the two loops.
// Preferred idiom for iterating over a collection
for (Element e : c) {
doSomething(e);
}
// No for-each loop or generics before release 1.5
for (Iterator i = c.iterator(); i.hasNext(); ) {
doSomething((Element) i.next());
}
Use this idiom below if the loop test involves a method invocation that is guaranteed to return the same result on each iteration.
for (int i = 0, n = expensiveComputation(); i < n; i++) {
doSomething(i);
}
It has two loop variables, i and n, both of which have exactly the right scope. The second variable, n, is used
to store the limit of the first, thus avoiding the cost of a redundant computation on every iteration.
- Keep methods small and focused .
If you combine two activities in the same method, local variables relevant to one activity may be in the scope of the code performing the other activity. To prevent this from happening, simply separate the method into two: one
for each activity.
Summary
By minimizing the scope of local variables, you increase the read-ability and maintainability of your code and reduce the likelihood of error.
Effective Java 45 Minimize the scope of local variables的更多相关文章
- Effective Java 13 Minimize the accessibility of classes and members
Information hiding is important for many reasons, most of which stem from the fact that it decouples ...
- Effective Java 15 Minimize mutability
Use immutable classes as much as possible instead of mutable classes. Advantage Easy to design, impl ...
- Effective Java Index
Hi guys, I am happy to tell you that I am moving to the open source world. And Java is the 1st langu ...
- 《Effective Java》读书笔记 - 8.通用编程
Chapter 8 General Programming Item 45: Minimize the scope of local variables local variables应该在他们要被用 ...
- Effective Java 目录
<Effective Java>目录摘抄. 我知道这看起来很糟糕.当下,自己缺少实际操作,只能暂时摘抄下目录.随着,实践的增多,慢慢填充更多的示例. Chapter 2 Creating ...
- 【Effective Java】阅读
Java写了很多年,很惭愧,直到最近才读了这本经典之作<Effective Java>,按自己的理解总结下,有些可能还不够深刻 一.Creating and Destroying Obje ...
- Effective Java 第三版——45. 明智审慎地使用Stream
Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...
- [Effective Java]第八章 通用程序设计
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- Effective Java 第三版——27. 消除非检查警告
Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...
随机推荐
- css3照片墙+曲线阴影
css3照片墙+曲线阴影 最近在学习jquery,晚上想复习下以前学过的知识,看到网上有关于css3照片墙的,感觉挺好玩的,就做了做.(以下图片均来自网络) 一.css3照片墙 html部分: < ...
- MySQL 备份与还原详解
相关阅读: MySQL备份和恢复具体实施 http://www.linuxidc.com/Linux/2012-12/76257.htm MySQL备份与恢复的三种方法总结 http://www.li ...
- 译:c#生成条码的web控件
译文:http://www.codeproject.com/Tips/846860/Csharp-Barcode-Generator-Web-Control 在asp.net的web页用c#的web控 ...
- .net中以传引用的方式 向方法中传参数
CLR(CommonLanguageRuntime)公共语言运行时,允许以传引用而非传值的方式传递参数.在C#中,这是用关键字 out 和ref来做到的. 从CLR角度来看,这两个关键字没什么区别,生 ...
- 一个App的界面设计流程是怎么产生的
作者:候佩雯链接:http://www.zhihu.com/question/27088793 完整的流程,分层次设计,自下而上去完成: 策略层,定义产品使命.价值.目标人群 愿景/功能层:定义核心场 ...
- Python入门笔记(18):Python函数(1):基础部分
一.什么是函数.方法.过程 推荐阅读:http://www.cnblogs.com/snandy/archive/2011/08/29/2153871.html 一般程序设计语言包含两种基本的抽象:过 ...
- 个人信息管理PIM——密码管理工具软件
密码管理工具 以KeePass为主,结合LastPass在线浏览器网页密码.有钱银可以考虑1Password. KeePass LastPass 1Password 价格费用 免费开源 普通版:免费 ...
- spring的懒加载
在spring的IOC容器中,可以通过设置<beans default-lazy-init="XXX"></beans>来设置是否为懒加载模式,懒加载的意思 ...
- sso demo ( cas )
1. generate keystore command : keytool -genkey -alias testtomcat -keyalg RSA -keystore "C:\User ...
- 设计模式总结篇系列:观察者模式(Observer)
观察者模式中通常有两个基本的概念主题:观察者和被观察者.当被观察者状态发生改变时,需要通知相应的观察者,当然,每个被观察者所对应的观察者可能不知一个,他们之间是1:n的关系.用专业一点的术语对观察者模 ...