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年出版,到现在已经将 ...
随机推荐
- CSS程序思想
CSS的设计思想,比如:CSS预处理器.CSS对像(OOCSS).SMACSS.Atomic设计和OrganicCSS等 一.CSS预处理器最重要的功能: 1.连接: ...
- jQuery的 delegate问题
习惯了bind,用惯了live,就不习惯delegate了呀有木有... 支持为动态生成的标签元素绑定事件也许就live和delegate了吧,不过新版本已经不支持live了,只有delegate d ...
- 设置数据库为SINGLE_USER模式,减少锁定时间
--SQL Server开启READ_COMMITTED_SNAPSHOT ----设置数据库为SINGLE_USER模式,减少锁定时间 ALTER DATABASE ENDV_SOA SET SIN ...
- MySQL主从复制与读写分离 --非原创
原文出处:http://www.cnblogs.com/luckcs/articles/2543607.html MySQL主从复制(Master-Slave)与读写分离(MySQL-Proxy)实践 ...
- html+css—two
1.滚动字幕(不常用) 默认状态:<marquee>向左移动</marquee> //-- hspasc滚动区域height滚动字幕高度 <marquee height= ...
- 【C#进阶系列】07 常量和字段
常量 常量总是被视为静态成员. 常量其实可以不限于基元类型,但是必须初始化为null.(我觉得这个点知道和不知道都一样,我已经自动从脑海中忽略了.很多时候在我这个人眼中,艰涩的代码和垃圾代码,其实没有 ...
- 用C#打开文件对话框的方法和简单使用的程序
- Linux服务器时间同步方法
一般稍微大点的项目都会部署到好几台服务器做集群,同一个应用可能部署到几台服务器上,而处理业务中必须让不同的服务器上时间保持一致,这就需要进行服务器间的时间同步.我的做法是: 1,选择其中一台对外网开放 ...
- Hibernate的缓存技术详解
转载注明出处:http://www.cnblogs.com/xiaoming0601/p/5882980.html 一.什么是缓存: 并不是指计算机的内存或者CPU的一二级缓存:缓存是指为了降低应用程 ...
- H5调用Android拨打电话
1.AndroidAndJSInterface.java class AndroidAndJSInterface { /** * 该方法将被js调用,用于加载数据 */ @JavascriptInte ...