Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Jochen Ott edited this page Feb 3, 2015 · 4 revisions

Code Conventions

Header Files

  • protect against multiple inclusion via #pragma once
  • only include necessary headers. For header files, it might be enough to forward-declare or include core/include/fwd.h for many classes
  • do not use using namespace in header files; use that only in source files

Memory

Make use of smart pointers for managing memory explicitly. A good choice is std::unique_ptr or (for shared ownership) std::shared_ptr. Remember you can create std::vectors (or other containers) of smart pointers, if necessary. In particular, do that if the actual type is not known at declaration time and only the base class is known, e.g. for abstract classes such as AnalysisModule.

For example, you could write quite generically in your AnalysisModule:

// in the class:
std::vector<std::unique_ptr<uhh2::AnalysisModule>> modules;

// in the constructor, add new modules:
modules.emplace_back(new JetCorrector(JERFiles::PHYS14_L123_MC));

// in process, call; all modules:
for(auto & m : modules){
   m->process(event);
}

Documentation

Document what each class and each function does, including a description of all input / output parameters. If applicable, also describe which event input is used or modified. Also document what happens in the case of an error.

For AnalysisModules document which Event content is read, written, or modified and whether some input / output is optional. State which options from the Context class are interpreted (if any) and what the allowed values are.

Classes

Use nouns as class names.

Be minimalistic for both data and methods: Only declare data members which are actually used and only implement methods which are used.

Hide details of the implementation by exposing no implementation-dependent data structures via the public interface.

Methods

Use verbs as method names. An exception is getter methods: they should be named after what they get (without the 'get' itself). Mark methods as const whenever possible.

Function Arguments

Be minimalistic: only use arguments actually needed.

The simpler, the better: avoid using more advanced constructs (such as accepting smart pointers) if a simpler construct (such as accepting a bare pointer of reference) will also do. To judge what is "simpler" consider how many different classes are involved in the declaration and how easy it is for a caller to get these. For example, it is always easier for a caller to get a reference than a smart pointer, as the former can be trivially constructed from the latter.

As function input arguments, use pass-by-value for non-compound types (int, float, double, etc.) and const-reference for compound types (vectors, strings, Jets, Leptons, etc.). Only if the argument is optional, pass by pointer-to-const.

To return the result, use the return value of the function if possible. Alternatively, accept a parameter by reference which is modified, e.g. the Event container. Only if the argument is optional, pass by pointer.

Only pass smart pointers if ownership is transferred / shared between caller and callee. This can be the case for a constructor where the object takes (shared or exclusive) ownership of one or more objects. For functions, it is almost always better to use references, const-references, or raw pointers instead of smart pointers, as discussed above.

Formatting

Use spaces only (no tabs), preferentially 4 spaces per indentation level.

Clone this wiki locally