Skip to content

Variables Scope, Functions, and Namespaces

williamfgc edited this page May 4, 2017 · 22 revisions
  1. Local scope variables and arguments in function declaration: first letter must be lower case.

    int rank = 0;
    double length = 10.5;
    void Foo( int arg1, double& arg2 );
  2. Variable declaration: declare variables right when they are needed, not at the beginning (e.g. avoid Fortran style) of a section of code.

    • Don't
      int  rank;
      // ...Many lines of code 
      rank = Foo( );
    • Do
      int  rank = Foo( );
  3. Function Names: start with an upper case letter.

    • Don't
      void set_transform( const int myNumber );
      std::string getStreamName( const Stream &stream );
    • Do
      void SetTransform( const int myNumber );
      std::string GetStreamName( const Stream &stream );
  4. Lambda functions naming: use hungarian notation with the prefix lf_

    • Don't
      auto SetValue = [ ]( const double value )
      {
         ...
      };
    • Do
      auto lf_SetValue = [ ]( const double value )
      {
         ...
      };
  5. adios namespace: all code must be enclosed in the namespace adios.

    • Don't
      class  Derived :  public  Base 
      { 
          ...
      };
    • Do
      namespace adios 
      {
      
      class Derived :  public  Base 
      { 
          ...
      };
      ...
      }   // end namespace adios  
      
  6. Avoid the using namespace clause: use the namespace:: explicitly. This prevents name conflict and allows identifying the function/variable source.

      • Exception: fixed size integers in ADIOSTypes.h (e.g. uint64_t, uint8_t), no need for the namespace std::
      • Don't
        #include <iostream>
        
        using namespace std;
        ...
        cout << "My message here\n";
        
      • Do
        #include <iostream>
        ...
        std::cout << "My message here\n";
        
        
    • auto keyword: it must not interfere with understanding the logic of the code (don't abuse its use).

      • Don't replace primitive or STL types
        // not obvious, could be short, int, unsigned int  
        auto rank = 1;  
        //not obvious, could be float or double   
        auto height = 2.5;  
        // not obvious, type of m_Weights not known  
        auto &amp; weights = myClass.m_Weights;  
        
      • Do use primitive or STL types for clarity, rules for const and references & apply. Use auto for iterators.
        const unsigned int rank = 1;  
        double height = 2.5;  
        std::vector<double>& weights = myClass.m_Weights;
        // OK, the STL find function returns an iterator    
        auto itMap = myMap.find("myName");
    • Passing variables: Always pass by value primitives ( const int, double ), small std::string (<= 15 characters), and STL iterators if only a copy is needed. The latter doesn't apply when the original value needs to be changed beyond the scope of a function. For larger objects, pass by reference ( ADIOS & adios, std::vector < std::string >& names ). These rules also apply to the auto keyword.