Link to discussion forum: https://community.openpbs.org/t/openpbss-c-coding-standards/2315
This will be added to PBS Pro Coding Standards
Follow the C Coding Standards, then look here for C++ specific guidelines.
File Extensions
Source files should use .cpp
Header files should use .hpp
Because we a mixed C/C++ project, it is helpful to know which header files we can use in a C file. Therefore, a C header should have a different extension than a C++ header.
.hpp is chosen because it easily attributed to .cpp files.
From the Boost FAQ:
File extensions communicate the "type" of the file, both to humans and to computer programs. The '.h' extension is used for C header files, and therefore communicates the wrong thing about C++ header files... Using '.hpp' unambiguously identifies it as C++ header file, and works well in actual practice. (Rainer Deyke)
Whitespace
We use tabs to be consistent with our C code.
Because we use tabs, we can’t use partial indents.
We don’t want to have a ton of horizontal white space making our lines longer than it has to be.
Classes will not have an indent before public
, protected
and private
// Bad class MyClass { public: MyClass(); ~MyClass(); private: int count; }; // Good class MyClass { public: MyClass(); ~MyClass(); private: int count; };
Namespaces will not cause a level of indentation
namespace pbs { // Bad int add(int x, int y); // Good int add(int x, int y); }
Names and Orders of Includes
pbs_config.h if needed
System and STL headers
pbs headers
Some good things to keep in mind
Casting
Do not use C-style casts; instead use C++ style casts.
// Bad int *p = (int *)malloc(sizeof(int)); // Good int *p = static_cast<int *>(malloc(sizeof(int)));
Note: Void pointers can't be assigned to non-void pointers without a cast.
Use const char *
whenever possible
If you're not changing a char *
argument, use const
in the function definition
// Bad void logmsg(int sev, char *msg) { printf("%d\t%s\n", sev, msg); return; } // Good void logmsg(int sev, const char *msg) { printf("%d\t%s\n", sev, msg); return; }
Prefer nullptr
to NULL
NULL
is often just defined 0, nullptr
is a pointer type pointing to the address 0x0.
// Bad int *p = NULL; // Good int *p = nullptr;
auto is awesome
A lot of types can be very long strings, such as classes with templates. auto
lets you save some typing by deducing the type from the right-hand-side.
// bad std::vector<MyClass> list = std::vector<MyClass>(); // good auto list2 = std::vector<MyClass>();
Namespaces
Using using namespace <name>
is not a good idea, as it can pollute the global namespace. Instead, use the namespace prefix.
// Bad using namespace std; auto vec = vector<int>(); // Good auto vec = std::vector<int>();
If you’re in the pbs
namespace, don’t create another namespace prefixed with pbs_
// Bad namespace pbs { namespace pbs_util { int count_spaces(const char *); } } int spaces = pbs::pbs_util::count_spaces(" "); // Good namespace pbs{ namespace util { int count_spaces(const char *); } } int spaces = pbs::util::count_spaces(" ");
Unique Pointers
You can find an example of unique pointers here: Unique Pointer Example