-
Notifications
You must be signed in to change notification settings - Fork 127
Variables Scope, Functions, and Namespaces
-
Local scope variables and arguments in function declaration: Use camelCase for local variables and function arguments.
int rank = 0; double length = 10.5; std::complex<double> partialSum(0.0, 0.0);
and
void Foo( int first, double& secondArg );
-
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( );
-
Don't
-
Function naming: Use PascalCase, also known as UpperCamelCase, for function names (stand alone or member).
-
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 );
-
Don't
-
Lambda functions naming: use hungarian notation with the prefix
lf_
-
Don't
auto SetValue = [ ]( const double value ) { ... };
-
Do
auto lf_SetValue = [ ]( const double value ) { ... };
-
Don't
-
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
-
Don't
-
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";
- Exception: fixed size integers in ADIOSTypes.h (e.g.
-
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 & weights = myClass.m_Weights;
-
Do use primitive or STL types for clarity, rules for
const
and references&
apply. Useauto
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");
-
Don't replace primitive or STL types
-
Passing variables: Always pass by value primitives (
const int, double
), small std::string objects (<= 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 theauto
keyword.