zoukankan      html  css  js  c++  java
  • Effective C++ 笔记 —— Item 26: Postpone variable definitions as long as possible.

    This suggests the real meaning of "as long as possible" in this Item’s title. Not only should you postpone a variable's definition until right before you have to use the variable, you should also try to postpone the definition until you have initialization arguments for it. By doing so, you avoid constructing and destructing unneeded objects, and you avoid unnecessary default constructions. Further, you help document the purpose of variables by initializing them in contexts in which their meaning is clear.

    // finally, the best way to define and initialize encrypted
    std::string encryptPassword(const std::string& password)
    {
        using namespace std;
        if (password.length() < MinimumPasswordLength) // import std and check length
        {
            throw logic_error("Password is too short");
        }
        
        string encrypted(password); // define and initialize via copy constructor
        encrypt(encrypted);
        return encrypted;
    }

    "But what about loops?" you may wonder. If a variable is used only inside a loop, is it better to define it outside the loop and make an assignment to it on each loop iteration, or is it be better to define the variable inside the loop? That is, which of these general structures is better?

    // Approach A: define outside loop 
    Widget w;
    for (int i = 0; i < n; ++i) 
    {
        // w = some value dependent on i; 
        // ...
    }
    
    
    // Approach B: define inside loop
    for (int i = 0; i < n; ++i) 
    {
        // Widget w(some value dependent on i);
        // ...
    }

    In terms of Widget operations, the costs of these two approaches are as follows:

    • Approach A: 1 constructor + 1 destructor + n assignments.
    • Approach B: n constructors + n destructors.

    For classes where an assignment costs less than a constructordestructor pair, Approach A is generally more efficient. This is especially the case as n gets large. Otherwise, Approach B is probably better. Furthermore, Approach A makes the name w visible in a larger scope (the one containing the loop) than Approach B, something that’s contrary to program comprehensibility and maintainability. As a result, unless you know that (1) assignment is less expensive than a constructor-destructor pair and (2) you’re dealing with a performance-sensitive part of your code, you should default to using Approach B.

    Things to Remember:

    • Postpone variable definitions as long as possible. It increases program clarity and improves program efficiency.
  • 相关阅读:
    Android开发环境下关于如何导出手机通讯录数据库【Written By KillerLegend】
    Win+R快速打开你的应用程序
    public void onItemClick(AdapterView arg0, View view, int position,long arg3)详解【整理自网络】
    Adapter的getView方法详解
    LayoutInflater中四种类型inflate方法的介绍
    程序员电脑桌面,哪一张触动了你?
    o​r​a​c​l​e​ ​O​D​B​C​配​置 图形界面
    C#String与string大小写的区别
    怎么使用FlashFXP上传网站
    C#操作Excel文件
  • 原文地址:https://www.cnblogs.com/zoneofmine/p/15247810.html
Copyright © 2011-2022 走看看