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年出版,到现在已经将 ...
随机推荐
- [python]自问自答:python -m参数?
python -m xxx.py 作用是:把xxx.py文件当做模块启动 但是我一直不明白当做模块启动到底有什么用.python xxx.py和python -m xxx.py有什么区别! 自问自答: ...
- Laravel 5 事件的使用
事件类通常被保存在 app/Events 目录下,而它们的处理程序则被保存在 app/Handlers/Events 目录下. 事件的创建 下面我们用artisan来创建一个事件,比如叫CqhTest ...
- 阅读Nosql代码有感
这一年总得来说,读书的时间不多.一是因为时间啥关系,这一年一直在跟着项目走,或者被项目牵着走,几乎所有的时间和精力全部被拴在几个项目上:不过所幸今年创业失败,又回去上班了,时间相对空余了一些. 双十一 ...
- sublime 插件zen coding
sublime的插件Zen Coding是一个编写html的神器,现在已经更名为Emmet了. 在sublime中的package需要搜索的是Emmet 相关网站: 官网 Zen Coding: 一种 ...
- CentOS6.5菜鸟之旅:中文编辑器忍痛放弃Sublime
一.前言 Windows下习惯使用Sublime作为编辑器,谁知道Linux下的Sublime是如此不照顾中文用户,找了N久终于找到一个蹩脚的解决方案,于是我毅然决然地加入Vim的阵营. 二.苦苦追寻 ...
- Week2 Bing词典Android客户端案例分析
一.软件调研 运行平台:Android 4.4.4 必应版本:5.2.2 1.bug发现 1.1 bug标题:单词挑战无法加载和刷新 bug详细描述:学习界面中的单词挑战模块,点击后没有任何反映,并且 ...
- [C#] 语法之Attribute
在c#中,定义类的成员,可以定义Property称为属性.Attribute就称为特性. 在FCL中,有内置的Attribute.如: Condition[Attribute]:在什么条件可以调用.( ...
- 构建之法 第6~7章读后感和对Scrum的理解
第六章-敏捷流程 第六章主要详细介绍了敏捷流程,在软件工程范畴里,“敏捷流程”是一系列价值观和方法论的集合.这一章以敏捷流程的Scrum方法论而展开,而敏捷流程的精髓就是在于快速的交付. 敏捷开发的流 ...
- C#中得到两个数百分比 (转)
//此方法得到的百分比后小数太多,不行double percent=Convert.ToDouble(2)/Convert.ToDouble(34); string result=(percent*1 ...
- Aspose.Word 操作word表格的行 插入行 添加行
rows.insert或rows.add前row必须有单元格cell private void button3_Click(object sender, EventArgs e) { ...