Principle

  1. The most powerful technique for minimizing the scope of a local variable is to declare it where it is first used.
  2. 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.

  3. prefer for loops to while loops

    Advantage

    1. The loops are completely independent, so there's no harm in reusing the element (or iterator) variable name.
    2. 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.

  4. 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的更多相关文章

  1. 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 ...

  2. Effective Java 15 Minimize mutability

    Use immutable classes as much as possible instead of mutable classes. Advantage Easy to design, impl ...

  3. 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 ...

  4. 《Effective Java》读书笔记 - 8.通用编程

    Chapter 8 General Programming Item 45: Minimize the scope of local variables local variables应该在他们要被用 ...

  5. Effective Java 目录

    <Effective Java>目录摘抄. 我知道这看起来很糟糕.当下,自己缺少实际操作,只能暂时摘抄下目录.随着,实践的增多,慢慢填充更多的示例. Chapter 2 Creating ...

  6. 【Effective Java】阅读

    Java写了很多年,很惭愧,直到最近才读了这本经典之作<Effective Java>,按自己的理解总结下,有些可能还不够深刻 一.Creating and Destroying Obje ...

  7. Effective Java 第三版——45. 明智审慎地使用Stream

    Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...

  8. [Effective Java]第八章 通用程序设计

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  9. Effective Java 第三版——27. 消除非检查警告

    Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...

随机推荐

  1. VM不能连入局域网

    如果选了Host-only,那么虚拟机与跑虚拟机的宿主就无法连通了. 可以选用Bridged模式,那么虚拟机与跑虚拟机的主机连通了

  2. 前端比较好的学习资料(包括js和css)以及 最全前端资源汇集

    js详细资料: http://javascript.ruanyifeng.com/ 『引』最全前端资源汇集: 来源:http://www.jeffjade.com/2016/03/30/104-fro ...

  3. 轻量级.NET ORM、高性能.NET ORM 之 SqlSugar 开源ORM - ASP.NET

    3.0最新API: http://www.cnblogs.com/sunkaixuan/p/5911334.html 1.前言/Preface SqlSugar从去年到现在已经一年了,版本从1.0升到 ...

  4. CSS3魔法堂:背景渐变(Gradient)

    一.前言 很久之前就了解过CSS3的线性渐变(Linear-Gradient),这段时间决定进一步认知这一特性,以下笔记以便日后查阅. 二.CSS3的各种背景渐变   1. 线性渐变 示例——七彩虹 ...

  5. 关于Latch争用

    Latch是什么     Latch是SQL Server引擎保证内存中的结构的一致性的轻量同步机制.比如索引,数据页和内部结构(比如非叶级索引页).SQL Server使用Buffer Latch保 ...

  6. HTTP请求响应报文&&相关状态码&&GET_POST请求方法 总结

    HTTP请求报文: 一个HTTP请求报文由四个部分组成:请求行.请求头部.空行.请求数据 1.请求行   请求行由请求方法字段.URL字段和HTTP协议版本字段3个字段组成,它们用空格分隔.比如 GE ...

  7. 全新重装win8.1系统后 配置开发及办公环境步骤

    全新重装win8.1系统后 配置开发及办公环境步骤 这两天,系统因配置开发环境出错,重装了一下,为日后方便,故此记录系统配置流程,防日后重装系统计划不周. 安装前,对照步骤,准备好下列安装文件. 0. ...

  8. BI之SSAS完整实战教程6 -- 设计维度、细化维度上:创建维度定义特性关系

    前面我们使用过数据源向导.数据源视图向导.Cube向导来创建相应的对象. 本篇我们将学习使用维度向导来创建维度. 通过前面几个向导的学习,我们归纳一下共同点,主要分成两步 1. 使用某种对象类型的向导 ...

  9. 那些教程没有的php3-命名空间

    php.net (PHP 5 >= 5.3.0, PHP 7) 定义命名空间 虽然任意合法的PHP代码都可以包含在命名空间中,但只有以下类型的代码受命名空间的影响,它们是:类(包括抽象类和tra ...

  10. FlexPaper 2.2.1介绍与提取嵌入的文档

            源起看到某个公司内网的公文使用FlexPaper组件来显示文档,在这儿是GoogleCode Project的主页, 还有现在的官方主页.目前FlexPaper是个开源项目,GPLv3 ...