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. 帮助你在 Photoshop 中轻松实现长阴影效果的工具

    扁平化设计正被用于各个主流的移动操作系统以及许多流行的网站,这是一个不断上升的趋势. 这种趋势有其分支,其中之一是“长阴影 “的效果在元素上的使用. 采用角度为 45 度的投影,给对象添加了一份立体感 ...

  2. 使用WinDbg调试SQL Server查询

    上一篇文章我给你介绍了WinDbg的入门,还有你如何能附加到SQL Server.今天的文章,我们继续往前一步,我会向你展示使用WinDbg调试SQL Server查询需要的步骤.听起来很有意思?我们 ...

  3. HTML解析类 ,让你不使用正则也能轻松获取HTML相关元素 -C# .NET

    功能: 1.轻松获取指元素HTML元素. 2.可以根据属性标签进行筛选 3.返回的都是Llist强类型无需转换 用过XElement的都知道 用来解析XML非常的方便,但是对于HTML的格式多样化实在 ...

  4. MySqlConnection 并发连接的问题

    最近在做项目的过程中遇到一个MySql在并发时初始化的问题,场景是这样子的: 我在Job中设定在同一时间点启动多个操作来访问数据库更新数据,结果在创建连接的时候抛出下面的问题: Note that w ...

  5. 程序代码记Log

    private static object s_lock = new object(); public static void TraceLog(string message, string logF ...

  6. 一、HTTPServer,RequestHandler,ServerHandler,Handler

    1.      HTTPServer,RequestHandler,ServerHandler,Handler 1.1       基本概念 HTTPServer主要是对传输控制层HTTP,TCP,S ...

  7. 软件测试之构建测试---BVT

    1. 构建的基本流程: a. 开发人员在他们的个人计算机上编写源代码文件 b. 他们将编写好的文件存放在一个统一集中的地方,构建组将所有的源代码编译成可以在计算机上运行的二进制文件,且用安装工具把各种 ...

  8. eclipse中去掉Js/javsscript报错信息

    1.首先在problem>errors中删除所有js错误: 如下图 2.然后再勾选掉javascript Validator: 3.clean下项目吧,你会发现再也不出现js红叉叉了,哈哈.

  9. Winform开发框架之客户关系管理系统(CRM)的报价单和销售单的处理

    在前面介绍了很多CRM相关的界面和实现思路的随笔文章,本篇继续介绍一下系统中用到的一些经验和技巧片段.本篇随笔主要介绍客户关系管理系统(CRM)的报价单和销售单的处理界面效果,使用列表内置的选择代替弹 ...

  10. Winform开发框架之权限管理系统的改进

    权限管理系统,一直是很多Mis系统和一些常见的管理系统所需要的,所以一般可以作为独立的模块进行开发,需要的时候进行整合即可,不需要每次从头开发,除非特殊的系统需求.我在Winform开发框架介绍中的随 ...