Skip to content

Variables Scope, Functions, and Namespaces

williamfgc edited this page May 1, 2017 · 22 revisions
  1. Local scope variables and function arguments: first letter must be lower case.
    •  int  rank  = 0;
    •  double  length = 10.5;
  2. Variable declaration: declare variables right when they are needed, not at the beginning (e.g. avoid Fortran style).

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

    • void  Transform( int  myNumber );
    • std::string  GetStreamName( const Stream & stream );
  4. Lambda functions: use hungarian notation with prefix lf_ in the function name:

    • 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. using namespace: avoid the " using namespace foo" clause, instead use the namespace explicitly. This prevents name conflict and allows identifying the function/variable source. Use the namespace adios in all developed code.

    • Exception: fixed size integers in ADIOSTypes.h (e.g. uint_64_t ), no need for the namespace std::
    • Don't
      •  using namespace  std;
        ...
        cout  << "My message here\n";
    • Do
      • std:: cout  << "My message here\n";
  7. 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
      •  auto  rank = 1;    // not obvious, could be short, int, unsigned int
        auto height = 2.5; //not obvious, could be float or double auto & weights = myClass.m_Weights; // not obvious, type of m_Weights not known
    • 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 & weights = myClass.m_Weights;
        auto  itMap = myMap.find("myName"); // OK, the STL find function returns an iterator
  8. Passing variables: Always pass by value primitives ( const int , double ), short std::string (<= 15 characters), and STL iterators if 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.