Programming 版 (精华区)
发信人: netiscpu (说不如做), 信区: Programming
标 题: C++ 编程准则与忠告 之 Variables
发信站: 紫 丁 香 (Sun Jul 26 11:21:58 1998), 转信
11 Variables
Back to index
Rule 38 Variables are to be declared with the smallest possible
scope.
Rule 39 Each variable is to be declared in a separate declaration
statement.
Rule 40 Every variable that is declared is to be given a value before
it is used.
Rule 41 If possible, always use initialization instead of assignment.
A variable ought to be declared with the smallest possible scope to
improve the readability of the code and so that variables are not
unnecessarily allocated. When a variable that is declared at the
beginning of a function is used somewhere in the code, it is not easy
to directly see the type of the variable. In addition, there is a risk
that such a variable is inadvertently hidden if a local variable,
having the same name, is declared in an internal block.
Many local variables are only used in special cases which seldom
occur. If a variable is declared at the outer level, memory will be
allocated even if it is not used. In addition, when variables are
initialized upon declaration, more efficient code is obtained than if
values are assigned when the variable is used.
A variable must always be initialized before use. Normally, the
compiler gives a warning if a variable is undefined. It is then
sufficient to take care of such cases. Instances of a class are
usually initialized even if no arguments are provided in the
declaration (the empty constructor is invoked). To declare a variable
that has been initialized in another file, the keyword extern is
always used.
By always initializing variables, instead of assigning values to them
before they are first used, the code is made more efficient since no
temporary objects are created for the initialization. For objects
having large amounts of data, this can result in significantly faster
code.
Exception to Rule 38: No exceptions.
Exception to Rule 39: No exceptions.
Exception to Rule 40: No exceptions.
Exception to Rule 41: In certain special cases, a variable is assigned
the value of a complicated expression; it may then be unnecessary to
give the variable an initial value. See Example 44.
Example 44 Initialization instead of Assignment
// Do not do this!
// int i;
// ... 1022 lines of code
// i = 10;
int j = 10; // Better
class Special // Array of this class is used to initialize
{ // MyClass::complicated
public:
Special(); // Default constructor
int isValid() const;
int value() const;
};
const int Magic = 1066;
Special specialInit[Magic];
class MyClass
{
public:
MyClass( const char* init ); // Constructor
// ...
private:
String privateString;
int complicated;
};
// Do not do this! Inefficient code.
// Empty constructor + assignment operator called for privateString
//
// MyClass::MyClass( const char* init )
// {
// privateString = init;
// ...
// }
MyClass::MyClass( const char* init ) : privateString( init ) // Better
{
// Special case - complicated expression
for( int i = 0; i<Magic; i++ ) // No! You should enclose "for"
if ( specialInit[i].isValid() ) // loops in braces! See Rec. 25!
{
complicated = specialInit[i].value();
break;
}
}
--
Enjoy Linux!
-----It's FREE!-----
※ 来源:.紫 丁 香 bbs.hit.edu.cn.[FROM: mtlab.hit.edu.cn]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:8.255毫秒