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. ASP.NET高并发解决方案

    一.页面静态化: 二.运用WCF,WEB SERVICE等技术实现分布式业务逻辑处理及数据访问的能力: 三.页面缓存: 四.数据缓存: 五.建立图片文件服务器:

  2. DecimalFormat 中的 # 与 0 的区别(中文帮助文档中翻译可能是错误的)

    想对数字进行格式化的时候,可能会使用到 java.text.DecimalFormat 类.中文帮助文档中,有如下符号 位置 本地化 含义 0 数字 是 阿拉伯数字 # 数字 是 阿拉伯数字,如果不存 ...

  3. Winform开发框架之字典管理模块的更新,附上最新2013年全国最新县及县以上行政区划代码sql脚本

    在很多项目里面,字典管理是必备的项目模块,而这个又是比较通用的功能,因此可以单独做成一个通用字典管理,例如这个模块,可以通过集成的方式,使用在我的<Winform开发框架>.<WCF ...

  4. Dev TreeList设置焦点失败解决方法

    问题描述 对TreeList初始化之后,设置treelist的焦点节点时,发现每次初始化控件的时不能正确的绑定焦点节点,第二次点开treelist的时候才会正常的设置目标节点为焦点节点. 截图 解决方 ...

  5. Redis持久化-数据丢失及解决

    Redis的数据回写机制 Redis的数据回写机制分同步和异步两种, 同步回写即SAVE命令,主进程直接向磁盘回写数据.在数据大的情况下会导致系统假死很长时间,所以一般不是推荐的. 异步回写即BGSA ...

  6. ASP.NET中Session简单原理图

    刚学习Session,对session的理解相当肤浅,按照我的想法画了原理图,麻烦各位大神指正,谢了!

  7. 重新想象 Windows 8 Store Apps (51) - 输入: 涂鸦板

    [源码下载] 重新想象 Windows 8 Store Apps (51) - 输入: 涂鸦板 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 涂鸦板 通过 Poin ...

  8. 与众不同 windows phone (43) - 8.0 相机和照片: 镜头的可扩展性, 图片的可扩展性, 图片的自动上传扩展

    [源码下载] 与众不同 windows phone (43) - 8.0 相机和照片: 镜头的可扩展性, 图片的可扩展性, 图片的自动上传扩展 作者:webabcd 介绍与众不同 windows ph ...

  9. poi excel export 乱码

    1. Question Description: ~前端是get方式提交的,参数含有中文“测试” ~导出的excel,文件名正常, 而标题乱码 2. Solution: ~断点发现, 参数接收就是乱码 ...

  10. Python学习之Python简介

    Python简介 Python的由来 Python(英国发音:/ˈpaɪθən/ 美国发音:/ˈpaɪθɑːn/), 是一种面向对象.解释型计算机程序设计语言,它是吉多·范罗苏姆(Guide van ...