`
leonzhx
  • 浏览: 793382 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Item 45: Minimize the scope of local variables

阅读更多

1.  By minimizing the scope of local variables, you increase the readability and maintainability of your code and reduce the likelihood of error.

 

2.  The most powerful technique for minimizing the scope of a local variable is to declare it where it is first used.

 

3.  The scope of a local variable extends from the point where it is declared to the end of the enclosing block.

 

4.  Nearly every local variable declaration should contain an initializer.

 

5.  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, where it cannot yet be “sensibly initialized”.

 

6.  Prefer for loops to while loops, assuming the contents of the loop variable aren’t needed after the loop terminates. There’s no incentive to use different variable names in the two for loops. The loops are completely independent, so there’s no harm in reusing the element (or iterator) variable name.

 

7.  You should use the following idiom 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);
}

 

8.  A final technique to minimize the scope of local variables is to keep methods small and focused.

分享到:
评论

相关推荐

    Effective Java 3rd edition(Effective Java第三版英文原版)附第二版

    Item 57: Minimize the scope of local variables Item 58: Prefer for-each loops to traditional for loops Item 59: Know and use the libraries Item 60: Avoid float and double if exact answers are required...

    Discriminative Learning of Local Image Descriptors

    blocks for constructing descriptors which can be combined together and jointly optimized so as to minimize the error of a nearestneighbor classifier. We consider both linear and nonlinear transforms ...

    the effect of voltage dips oninduction motors

    It is often desirable to minimize the effect of the voltage dip on both the induction motor and more importantly on the process where the motor is used. In order for the user to achieve optimum ...

    Effective C#

    **Item 6: Understand the Relationships Among the Many Different Concepts of Equality** - **Concepts:** Value equality, reference equality, and custom equality. - **Explanation:** - **Value Equality:...

    A Unified Framework for the Study of Anti-Windup Designs

    bumpless transfer (AWBT) compensation to minimize the adverse eflects of any control input nonlinearities on closed loop performance". The resulting AWBT compensation is applicable to multivariable ...

    Effective[removed]更新 EffectiveJavaScript 到 JavaScript 新版

    Accustoming Yourself to JavaScript Chapter 2: Variable Scope Item 08: Minimize Use of the Global Object Item 09: Always Declare Local Variables Item 10: Avoid with Item 11: Get Comfortable with ...

    A.Philosophy.of.Software.Design.2018.4 (1).pdf

    This book addresses the topic of software design: how to ... You can apply the ideas in this book to minimize the complexity of large software systems, so that you can write software more quickly

    A New Optimal Hatch Filter to Minimize the Effects of Ionosphere Gradients for GBAS

    improve the accuracy of the pseudorange by reducing the noise in it and eliminating almost all the common errors between the user and the reference station, they also cause extra errors on account of...

    凸优化理论与应用-凸优化.ppt

    minimize f(x), subject to g_i(x) ≤ 0, i = 1, ..., m h_j(x) = 0, j = 1, ..., p 其中,x ∈ ℝⁿ为优化变量,f(x)为目标函数,g_i(x)和h_j(x)分别为不等式约束和等式约束。 2.局部最优解: 局部最优解是指在...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    Scoping Namespaces Nested Classes Nonmember, Static Member, and Global Functions Local Variables Static and Global Variables Classes Doing Work in Constructors Default Constructors Explicit ...

    Microsoft® .NET: Architecting Applications for the Enterprise

    Description: Make the right architectural decisions up front and improve the quality and reliability of your results. Led by two enterprise programming experts, you ll learn how to apply the patterns ...

    minimize(windows窗口最小化工具)

    "minimize"是一款专为Windows操作系统设计的窗口最小化工具,它的主要功能是将打开的窗口轻松快捷地最小化到桌面,以便用户更高效、整洁地管理和浏览各个应用程序。这款工具特别适合那些需要同时处理多个任务,且...

    Kanban for the Shopfloor

    The circulation phase focuses on the movement of Kanbans through the system. Important aspects include: - **When to Produce**: Production is triggered by the withdrawal of items from downstream ...

    essential skills of java

    - **Session Timeout:** Implementation of appropriate session timeouts to minimize exposure. 5. **Encryption and Data Protection:** - **Cryptographic Algorithms:** Knowledge of cryptographic ...

    Effective C# 2ed (Covers C# 4.0)

    See how optional parameters can minimize the number of method overloads (see Item 10) You’re already a successful C# programmer–this book will help you become an outstanding one. About the Author ...

    移动渐近线法

    % xval = Column vector with the current values of the variables x_j. % xmin = Column vector with the lower bounds for the variables x_j. % xmax = Column vector with the upper bounds for the variables ...

    matlab开发-minimize

    在MATLAB中,`minimize`是一个非常重要的优化工具,用于寻找函数的最小值。它提供了多种算法,包括全局和局部优化方法,适用于各种不同类型的优化问题。在本项目"matlab开发-minimize"中,重点是利用MATLAB内置的`...

    ECONOMETRIC_MODELS_with_MATLAB.pdf

    If a model is linear in the parameters, estimation is based on methods from linear algebra that minimize the norm of a residual vector. If a model is nonlinear in the parameters, estimation is based ...

    财务管理第十六章课件.pptx

    Chapter 16 of the "Financial Management" course discusses the concept of financial leverage and capital structure policy, which are crucial elements in determining the financial health and overall ...

    SNAP_Mobile_Game_Compliance_Testing_Guide_v1_2_en

    - **Document Scope and Audience**: The document is designed to provide guidance for developers and testers involved in the compliance testing of mobile games for the SNAP Mobile platform. It covers ...

Global site tag (gtag.js) - Google Analytics