Skip to content

Instantly share code, notes, and snippets.

@janpipek
Last active January 10, 2016 17:32
Show Gist options
  • Save janpipek/1d0842d9a2eecd314fc2 to your computer and use it in GitHub Desktop.
Save janpipek/1d0842d9a2eecd314fc2 to your computer and use it in GitHub Desktop.
Elimed C++ conventions.

These two files contain my idea of proper C++ coding conventions. Please feel free to comment below (and I can update).

#ifndef CODING_CONVENTIONS_HH // $ always add the "header guard"
#define CODING_CONVENTIONS_HH
#include <vector> // $ first include system headers
#include <G4SomeGeant4Type.hh> // $ then include Geant4 headers
#include "SomeElimedClass.hh" // $ then include headers from the current application
class G4UImessenger; // $ Don't import non-necessary headers in headers.
class G4LogicalVolume; // $ Use forward class declaration instead (possible for pointers and references)
// $ The following lines are a typical Doxygen comment for a class.
// $ For more details see http://www.stack.nl/~dimitri/doxygen/manual/commands.html
/**
* @short An example coding convention class.
*
* It is useless so this is a bit more elaborate description.
* Which can consist of multiple lines...
*
* @example
* auto cc = CodingConventions();
* cc->MakeAmazingStuff();
*
* @note This class is thread-safe. But contains no spiders.
*
* @todo Add some spiders
*/
class CodingConventions : public G4SomeGeant4Type
{
public:
// $ Always start the section with appropriate public/private/protected keyword (not indented)
CodingConvention(params...); // $ Constructor first
void DoSomething(); // $ Method names should mostly be English verb expressions
void FaiQualcosa(); // $ WRONG: No Italian names!
void UdelejNeco(); // $ WRONG: No Czech names!
void SetJoke(const G4String& joke); // $ Setters should start with Set, use const references for more complicated types
G4String GetJoke() const; // $ Getters should be const and start with Get
void SetJokeFunny(G4bool funny);
G4bool IsJokeFunny() const; // $ Boolean getters start with Is...
// $ Simple getters and single-line methods should be included in the class declaration.
G4int GetValue() const { return fValue; }
private:
G4double fNumber; // $ Start instance variable names with an "f"
// $ Unless the class is a special container (more a struct then?), make all instance variables protected or private
G4UImessenger* fMessenger;
G4LogicalVolume* fVolume1; // $ WRONG: Give it a real name!
G4LogicalVolume* fExit_window; // $ WRONG: Don't use underscores
G4LogicalVolume* fExtWnd; // $ WRONG: Non-standard abbreviations
// $ Use smart pointers instead of pointers if it can be done (tricky in Geant4)
std::shared<G4UImessenger> fMessenger;
}
#endif
#include "CodingConventions.hh" // $ Include the corresponding as the first line of the source file.
using namespace std; // $ It's okay to use "using" statement in source file
void CodingConvention::FaiQualcosa()
{
auto aMap = new map<G4String, shared_ptr<G4LogicalVolume>>; // $ Use auto if class name are complicated
G4LogicalVolume* notInitialized = nullptr; // $ Use nullptr for uninitialized pointers
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment