Skip to content

Instantly share code, notes, and snippets.

@dpasca
Last active June 26, 2025 10:29
Show Gist options
  • Save dpasca/5243278 to your computer and use it in GitHub Desktop.
Save dpasca/5243278 to your computer and use it in GitHub Desktop.
// GENERAL RULES:
// - Always favor `const`
// - Always favor `auto` over explicit types
// - Force default/zero init for POD class members (no surprises)
// - Always favor std::vector<> over malloc()
// - Minimize nesting -> favor `if (!x) return;` over `if (x) { ... } else { ... }`
// - Use clear separator before class definitions or functions implementation
// - Favor `#if 0` for block comments over C or C++ comments
// - Default to `size_t` for counters
// - Default to auto-preincrement (++i)
// Sample code below:
class Type; // forward declaration if possible
//==================================================================
class ClassName
{
size_t mMember = 0; // 'm' prefix for member variable
U8* mpPointer {}; // 'mp' prefix for member pointer
std::unique_ptr<Type> moUniqueObj; // 'mo' prefix for unique_ptr (Owned)
std::shared_ptr<Type> msSharedObj; // 'ms' prefix for shared_ptr
public:
// Dedicated 'Params' structure for classes with many parameters,
// especially if they need to be retained
struct Params
{
int param1 = 0;
float param2 = 0;
// ...
};
private:
const Params mPar; // keeps a copy of the ctor params (often useful)
public:
ClassName(const Params &par) : mPar(par) {}
~ClassName(); // destructor usually necessary only
// to use unique_ptr + forward declaration
// public method with Uppercase initial
void PublicMethod(int yoyo);
private:
// private method with lowercase initial
void privateMethod();
};
//==================================================================
// initial for local (static) function is lowercase
static void localFunction(int oneParam, int anotherParam)
{
}
//==================================================================
// initial for global function is Uppercase
void GlobalFunction(
// multi-line param list when more than 3-4 params
int oneParam,
int anotherParam,
int thirdParam,
size_t fourthParam)
{
// left value and right value are sometimes aligned with virtual tabs
// (never tab chars)
const auto dude = (float)oneParam; // C-style cast for simple cases
const auto longerDude = anotherParam;
// Minimize nesting
if (!dude)
return;
// Bigger chunk of code, not nested
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment