当前位置:有风信息港IT学院编程技术JAVA → 只要有可能就推迟变量定义

只要有可能就推迟变量定义

减小字体 增大字体 作者:有风IT学院  来源:有风信息港  发布时间:2008-1-13 9:37:42
  在极大程度上,为你的类(包括类模板)和函数(包括函数模板)提供正确的定义是战斗的关键性部分。一旦你得到正确的结果,相应的实现很大程度上就是直截了当的。但是仍然有一些注意事项需要当心。过早地定义变量会对性能产生拖累。过度使用强制转换会导致缓慢的,难以维护的,被微妙的 bug 困扰的代码。返回一个类内部构件的句柄会破坏封装并将空悬句柄留给客户。疏忽了对异常产生的影响的考虑会导致资源的泄漏和数据结构的破坏。过分内联化(inlining)会导致代码膨胀。过度的耦合会导致令人无法接受的漫长的建构时间。 这一切问题都可以避免。

  只要有可能就推迟变量定义

  只要你定义了一个带有构造函数和析构函数的类型的变量,当控制流程到达变量定义的时候会使你担负构造成本,而当变量离开作用域的时候会使你担负析构成本。如果有无用变量造成这一成本,你就要尽你所能去避免它。

  你可能认为你从来不会定义无用的变量,但是也许你应该再想一想。考虑下面这个函数,只要 password 的长度满足要求,它就返回一个 password 的加密版本。如果 password 太短,函数就会抛出一个定义在标准 C++ 库中的 logic_error 类型的异常(参见 Item 54):

  

  // this function defines the variable "encrypted" too soon

  std::string encryptPassword(const std::string& password)

  {

   using namespace std;

  

   string encrypted;

  

   if (password.length() < MinimumPasswordLength) {

    throw logic_error("Password is too short");

   }

   ... // do whatever is necessary to place an

   // encrypted version of password in encrypted

   return encrypted;

  }

  

  对象 encrypted 在这个函数中并不是完全无用,但是如果抛出了一个异常,它就是无用的。换句话说,即使 encryptPassword 抛出一个异常,你也要为构造和析构 encrypted 付出代价。因此得出以下结论:你最好将 encrypted 的定义推迟到你确信你真的需要它的时候:

  

  // this function postpones encrypted’s definition until it’s truly necessary

  std::string encryptPassword(const std::string& password)

  {

   using namespace std;

  

   if (password.length() < MinimumPasswordLength) {

    throw logic_error("Password is too short");

   }

  

   string encrypted;

   

   ... // do whatever is necessary to place an

   // encrypted version of password in encrypted

   return encrypted;

  }

  这一代码仍然没有达到它本可以达到的那样紧凑,因为定义 encrypted 的时候没有任何初始化参数。这就意味着很多情况下将使用它的缺省构造函数,对于一个对象你首先应该做的就是给它一些值,这经常可以通过赋值来完成我已经解释了为什么缺省构造(default-constructing)一个对象然后赋值给它比用你真正需要它持有的值初始化它更低效。那个分析也适用于此。例如,假设 encryptPassword 的核心部分是在这个函数中完成的:

  

  void encrypt(std::string s); // encrypts s in place

  那么,encryptPassword 就可以这样实现,即使它还不是最好的方法:

  

  // this function postpones encrypted’s definition until

  // it’s necessary, but it’s still needlessly inefficient

  std::string encryptPassword(const std::string& password)

  {

   ... // check length as above

  

   std::string encrypted; // default-construct encrypted

   encrypted = password; // assign to encrypted

  

   encrypt(encrypted);

   return encrypted;

  }

  

推荐文章:搞笑之可爱水果表情  清新素洁水仙壁纸集


  

[1] [2]  下一页