Developer Style Guide Example - larz7/larzworksamples GitHub Wiki

Formatting

Braces

Each brace needs its own line to line up with its matching brace. For example:

Car::getDistanceDriven(bool inKM)
{
	if (inKM)
	{
		return m_milesDriven * 1.64;
	}
	else 
	{
		return m_milesDriven;
	}
}

Indentation

Indentation may be done via tabbing or spaces, but in both cases indents should be 4 spaces wide. Contents of a namespace should be indented at one level more than its corresponding namespace keyword. Likewise, contents of a class should be indented one more level than the class keyword. This includes private, protected, and public. The case and default keywords should be indented within a switch, and break should be indented within a case or default.

Comments

Use // instead of /**/.

Whitespace

Put spaces between variables and operators but not between expressions and parenthesis. For example:

if (this == that)
{
	variable = that;
}

When inheriting from a parent class, put the parent class on the next line. For example:

class CompactCar :
	public Car
{
}

Naming Style

File Names

Source files should end with extension .cpp. Header files should end with extension .hpp. For both header and source files, name the file after the class, and capitalize using camel case. (e.g. example.cpp, example.hpp, ExampleClass.cpp)

Namespaces

Namespaces should be all in lowercase (e.g. std, package, utils, etc).

Classes & Structs

Classes and structs should use camel case for their names. (e.g. ExampleClass, LargeInteger, etc.).

Member Functions

Member functions should user lower camel case (e.g. myFunction, getValue, etc).

Class Member Variables & Struct Fields

Member variables should be indicated by using the m_ or sm_ prefix for non-static and static members, respectively, and use lower camel case for the rest of the name (e.g. m_foo, sm_staticValue, etc).

Syntactical Convention / Best Practices

The following are general guidelines for the code base.

  • In header files, use #ifndef, #define, and #endif to avoid inclusion problems. For instance:
	#ifndef _EXAMPLE_HPP_
	#define _EXAMPLE_HPP_
	
	// Code Here
	
	#endif	// _EXAMPLE_HPP_
  • When including header files, include:
    • standard libraries first (using < >)
    • other libraries next (using < >)
    • your module file last (using "")
  • Put a blank line between groups
  • Don't surround #include with an #ifndef - the header file should do that for you.
  • Prefer C++ style names, i.e. <cstdio> or <cmath> instead of <stdio.h> or <math.h>.
  • When using code within namespaces, do not use the using keyword. Instead, use the full name like std::vector<int> intVector or std::queue<char> charQueue.
  • Use smart pointers instead of new and delete if possible
  • Use const when appropriate
  • Throw exceptions only when something very bad is happening
  • Avoid using catch(…)
  • Structs should not contain methods
  • In general, only have one class per source file. Multiple classes may be declared in the same header file when it makes sense.

Cheat Sheet

Code Style Category Rule Example
Indentation Tab size 4 spaces
public, protected, private Indent
class foo
{
	public: 
		int hello;
}
Declarations within public, protected, private Indent
Statements within function body Indent
int myFunction()
{
     return 1;
}
Statements within blocks Indent
if (true)
{
    myBool = true;
}
Statements within switch Indent
switch(var)
{
    case 0:
        foo = 42;
        break;
    default:
        foo = 0;
}
Statements within case Indent
break statements relative to case Indent
Braces All statements / blocks Newline before each brace
Control Statements else in if … else statement Newline before else
if (true)
{
    //Simple if here
    return;
}
else if 
{
    var = 2;
    return;
}
else 
{
    doStuff();
    return;
}
catch in try … catch statement Newline before catch
else if On same line
simple if Use braces
Name Style Category Capitalization Example
Code Constant Uppercase
#define MY_CONSTANT
Variable Lower camel case
int localVariable
Class field Lower camel case with m_ or sm_ prefix
class Example
{
    static int sm_foo;
    int m_bar;
    void printHello();
}
Class method Lower camel case
Files C++ header file Upper camel case, *.hpp ExampleHeader.hpp
C++ source file Upper camel case, *.cpp ExampleSource.cpp

Example Code

#include <cmath>
#include <cstdio>
#include <strings>
#include <vector>

#include <Base/Library/Headerfile.hpp>
#include <Base/Log/Log.hpp>

#include "MyHeaderFile.hpp"
#include "My/Other/HeaderFile.hpp"

class Point
{
	public:
		Point(double x, double y) :
				x(x), y(y)
		{
		}

		double distance(const Point& other) const;
		int compareX(const Point& other) const;
		double x;
		double y;
};

double Point::distance(const Point& other) const
{
	double dx = x - other.x;
	double dy = y - other.y;

	return std::sqrt(dx * dx + dy * dy);
}

// This method compares this point with another point.
int Point::compareX(const Point& other) const
{
	if (x < other.x)
	{
		return -1;
	}
	else if (x > other.x)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

namespace foobar
{
	int foo(int bar) const
	{
		switch (bar)
		{
			case 0:
				++bar;
				break;
			case 1:
				--bar;
			default:
			{
				bar += bar;
				break;
			}
		}
	}
} // end namespace foobar

⚠️ **GitHub.com Fallback** ⚠️