Ellemtel C++ Style and Rules - Variables


11. Variables
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 constructorint 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;
         }
   }

  Need all your C++ source code be formatted and indented automatically?  Try C++ Source Code Formatter today.