Skip to content

Instantly share code, notes, and snippets.

@lefticus
Last active August 26, 2024 10:03
Show Gist options
  • Save lefticus/10191322 to your computer and use it in GitHub Desktop.
Save lefticus/10191322 to your computer and use it in GitHub Desktop.
C++ Coding Standards

C++ Coding Standards Part 1: Requirements

Style

Style guidelines are not overly strict. The important thing is that code is clear and readable with an appropriate amount of whitespace and reasonable length lines.

2 spaces indent level.

Tabs are not allowed, and a mixture of tabs and spaces is strictly forbidden. Modern autoindenting IDEs and editors require a consistent standard to be set.

int myFunction(bool t_b)
{
  if (t_b)
  {
    // do something
  }
}

{} are required for blocks.

Leaving them off can lead to semantic errors in the code.

// this compiles and does what you want, but can lead to confusing
// errors if close attention is not paid.
for (int i = 0; i < 15; ++i)
  std::cout << i << std::endl;

// the cout is not part of the loop in this case even though it appears to be
int sum = 0;
for (int i = 0; i < 15; ++i)
  ++sum;
  std::cout << i << std::endl;
  
// Correct
int sum = 0;
for (int i = 0; i < 15; ++i) {
  ++sum;
  std::cout << i << std::endl;
}

Keeps lines a reasonable length

// bad and hard to follow
if (x && y && myFunctionThatReturnsBool() && caseNumber3 && (15 > 12 || 2 < 3)) { 
}

// better
if (x && y && myFunctionThatReturnsBool() 
    && caseNumber3 
    && (15 > 12 || 2 < 3)) { 
}

Safety

Avoid raw memory access

Raw memory access, allocation and deallocation are difficult to get correct in C++ without risking memory errors and leaks. C++11 provides tools to avoid these problems.

// bad
MyClass *myobj = new MyClass;

// ...

delete myobj;


// Good
std::shared_ptr<MyClass> myobj = make_shared<MyClass>();
// ... 
// myobj is automatically freed for you whenever it is no longer used.

Avoid global data

this includes singleton objects

C++ Coding Standards Part 2: Guidelines

Style

Initialize Member Variables

with the member initializer list

// bad
class MyClass
{
public:
  MyClass(int t_value)
  {
    m_value = t_value;
  }

private:
  int m_value;
};

// good
class MyClass
{
public:
  MyClass(int t_value)
    : m_value(t_value)
  {
  }

private:
  int m_value;
};

Performance

Prefer pre-increment to post-increment

... when it is semantically correct. Pre-increment is faster then post-increment because it does not require a copy of the object to be made.

// Correct
for (int i = 0; i < 15; ++i)
{
  std::cout << i << std::endl;
}

// incorrect
for (int i = 0; i < 15; i++)
{
  std::cout << i << std::endl;
}

Const as much as possible

const tells the compiler that a variable or method is immutable. This helps the compiler optimize the code and helps the developer know if a function side effects. Also, using const & prevents the compiler from copying data unnecessarily.

// bad
class MyClass
{
public:
  MyClass(std::string t_value)
    : m_value(t_value)
  {
  }

  std::string get_value() 
  {
    return m_value;
  }

private:
  std::string m_value;
}

// good
class MyClass
{
public:
  MyClass(const std::string &t_value)
    : m_value(t_value)
  {
  }

  std::string get_value() const
  {
    return m_value;
  }

private:
  std::string m_value;
}
@NickPryorMe
Copy link

Very nice and detailed, thank you.

@slonorib
Copy link

Thank you :)

@tomkowz
Copy link

tomkowz commented Aug 2, 2018

Thanks!

@mojtaba-maleki
Copy link

Thank you,it was really helpful.

@vijaykumarkaripaka
Copy link

vijaykumarkaripaka commented Sep 19, 2018

nice..helpful

@Voxed
Copy link

Voxed commented Oct 4, 2018

This is great!

@shibly006
Copy link

Veeeery helpful.

@Bloofer
Copy link

Bloofer commented Dec 24, 2018

thank you:)

@alphaJohnny
Copy link

Great! thank you.

@FelixWeichselgartner
Copy link

Nice!

@Pendulun
Copy link

Thanks!

@PiggiesGoSqueal
Copy link

Thank you!

@SepidehAbadpour
Copy link

Thanks

@oxycoder
Copy link

oxycoder commented Jul 5, 2019

Should use string_view instead of const std::string &

@HernanRivasAcosta
Copy link

HernanRivasAcosta commented Jul 19, 2019

The link to Google's standard is broken (my guess is because Google Code is gone). This is the new one: https://google.github.io/styleguide/cppguide.html

Also dead is the sourceforge one, but I have no idea where that is.

@lefticus
Copy link
Author

lefticus commented Aug 6, 2019

I just noticed that people are commenting on this gist. This file has not been maintained in many years. All of this has been moved to the much better organized online C++ Best Practices book.

https://github.com/lefticus/cppbestpractices/blob/master/00-Table_of_Contents.md

@PiggiesGoSqueal
Copy link

I just noticed that people are commenting on this gist. This file has not been maintained in many years. All of this has been moved to the much better organized online C++ Best Practices book.

https://github.com/lefticus/cppbestpractices/blob/master/00-Table_of_Contents.md

Okay thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment