Skip to content

CodingStyle

Olena Hlushchenko edited this page Apr 25, 2018 · 15 revisions

Coding Style

General

C++

  • Split code in header and source file whereever possible to speed up the compiling process. The only exception are templated classes or functions.

  • Example header file ClassName.h

#pragma once

// include as few headers here as possible. Move as many of them as possible to the source file

class ClassName : public BaseClass
{
public:
	ClassName();
	void FunctionName(ComplexType const& parameterName2, int parameterName1=0);

private:
	int m_classMember = 0; // always initialise variables
};
  • Example source file ClassName.cc
#include "HiggsAnalysis/KITHiggsToTauTau/interface/ClassName.h" // use full paths

ClassName::ClassName() : 
	BaseClass(0),
	m_classMember(0) // initialise class members here instead of in the body
{
	// some more code
}

void ClassName::FunctionName(ComplexType const& parameterName2, int parameterName1)
{
	std::vector<int> vectorWithName = {0, 1, 2, 3};
	
	// space after for
	// no variables with just one letter as name (bad for searching)
	// comparison of variables with the same type (here vectorWithName.size() is an unsigned int or size_t)
	for (unsigned int index = 0; index < vectorWithName.size(); ++index)
	{ // new line
	
		// prefer LOG(...) over std::cout, because it is managed by the global logger (and is shorter)
		LOG(INFO) << vectorWithName.at(index);
		// use at(...) instead of [...] to access elements of a collection, because this throws a well defined exception if element does not exist
	}
	
	// space after if
	// always use the brackets {...} for the scope to avoid mistakes when switching between C++ and Python
	if (parameterName1 == vectorWithName.at(0))
	{
		// code
	}
	else if (parameterName1 == vectorWithName.at(1))
	{
		// code
	}
	else
	{
		// code
	}
}

Python

  • Example Python package packagename.py in a python directory
# -*- coding: utf-8 -*-

# initialise logger
import logging
import Artus.Utility.logger as logger
log = logging.getLogger(__name__)

class ClassName(BaseClass):
	
	def __init__(self):
		super(ClassName, self).__init__() # call upper class constructor
		self.class_member = 0
	
	def function_name(parameter_name1, parameter_name2):
		list_with_name = [0, 1, 2, 3]
		
		for element in list_with_name: // always start a new line for a new block
			log.info(str(element))
		
		if (parameter1 == list_with_name[0]):
			# code
		elif (parameter1 == list_with_name[1]):
			# code
		else
			# code
  • Example Python script script.py in a scripts directory (file should have execution rights)
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# initialise logger
import logging
import Artus.Utility.logger as logger
log = logging.getLogger(__name__)

# every script has an ArgumentParser and a logger
import argparse

parser = argparse.ArgumentParser(description="Description.", parents=[logger.loggingParser])
parser.add_argument("-s", "--single-argument", default=..., help="... [Default: %(default)s]")
parser.add_argument("-l", "--list-argument", nargs="+", default=..., help="... [Default: %(default)s]")
parser.add_argument("-b", "--bool-argument", default=False, action="store_true", help="... [Default: %(default)s]")

args = parser.parse_args()
logger.initLogger(args)

# code

Bash

export VARIABLE_NAME="test"

Additional tools

If you for somereason don't like to code in gedit you might want to set up sublime, eclipse or atom.

Editing Setting Eclipse editor in Aachen

  • open eclipse from terminal after soursing everything with some setki* shortcut
  • new project -> name, C++ proj -> Next
  • Makefile project -> Next
    • empty proj
    • linux GCC
    • Choose location
  • Advance settings
    • C++ General
      • Paths and Symbols -> Add -> Variables
        • $CMSSW_BASE/src
        • ROOTSYS/include
        • CMSSW_RELEASE_BASE/src
        • CMSSW_FWLITE_INCLUDE_PATH
  • that's it, just wait

Link to Twiki

Clone this wiki locally