0% found this document useful (0 votes)
35 views2 pages

General Important Points: C++ Coding Standards and Best Practices

C++ coding standards and best practices recommend (1) writing clear and consistent code with descriptive variable and function names, (2) keeping headers clean and minimal, and (3) using const wherever possible. Classes should (1) follow the rule of three for copy constructor, copy assignment, and destructor, (2) encapsulate data rather than using global data, and (3) use constructors to initialize. Code should also (1) avoid exposing private members, (2) be portable, and (3) avoid memory leaks.

Uploaded by

dhanyafb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views2 pages

General Important Points: C++ Coding Standards and Best Practices

C++ coding standards and best practices recommend (1) writing clear and consistent code with descriptive variable and function names, (2) keeping headers clean and minimal, and (3) using const wherever possible. Classes should (1) follow the rule of three for copy constructor, copy assignment, and destructor, (2) encapsulate data rather than using global data, and (3) use constructors to initialize. Code should also (1) avoid exposing private members, (2) be portable, and (3) avoid memory leaks.

Uploaded by

dhanyafb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

C++ Coding Standards and Best Practices

(Source: http://web.mit.edu/6.s096/www/standards.html)

General Important Points


1. Write in a clear and consistent coding style. all of your submitted code should be
formatted in clear way.
2. Code should be self-documenting. Variables should be sensibly named, function
names descriptive of their purpose. Reserve comments for places where clarification
is valuable: if you have a long piece of code and it's hard to tell what it does, consider
splitting it up into several functions whose names will describe what's going on.
3. Keep your headers clean. Put the absolute minimum required in your headers for
your interface to be used. Anything that can go in a source (.cpp) file should. Don't
#include any system headers in your .h files that aren't absolutely required in that file
specifically.
4. Don't expose your classes' private parts. Keep as much of your classes private as
possible. All data members should be private; mark them as such by using a leading
underscore: int _someVariable; you can then write the getter/setter functions for
this variable as int someVariable() const; and int& someVariable( int
value );. Avoid friend functions unless absolutely necessary.
5. Use const wherever possible. All member functions that do not modify their object
should be const. If your function takes a reference to an object and does not modify
that object, you should be passing a const reference.
6. Write portable code. Don't use any very compiler-specific features or depend on
long or unsigned types being a particular size. Prefer to use size_t for array
indexing, especially when dealing with the STL.
7. Don't leak memory. Every heap allocation using new should have a corresponding
delete.

Classes in C++

Follow the rule of three. If your class needs a non-trivial destructor, you should also
either implement or disable the copy constructor and copy assignment operators.
(furthermore, if you want your data to have the ability to be moved cheaply, also
define the move constructor and move assignment)

Don't use global data. Instead, encapsulate it in a class and design your interfaces
effectively.

Use the constructor to initialize.

Other

Do take advantage of the standard template library. Generally don't try to rewrite
data structures and algorithms that have already been implemented well in the
standard library unless you have a good reason.

You might also like