- Keep names concise and readable — Avoid variable and method names exceeding 40 characters to maintain clarity.
- Use descriptive names — Names should convey meaning without requiring additional comments.
- Casing for accessibility:
- Public members: use
PascalCase→ Example:FooName - Private & Protected members: Use
camelCase→ Example:fooName - Constants: Use
UPPER_CASE_WITH_UNDERSCORES→ Example:MAX_BUFFER_SIZE
- Public members: use
-
Avoid deeply nested
ifstatements-
Instead of:
if(a == b) { c(); }
-
Use:
if(a != b) return; c();
-
-
Use early exits to improve readability
-
Bad:
if(a > 0) { if(b > 0) { c(); } }
-
Good:
if(a > 0) return; if(b > 0) return; c();
-
-
Use ternary operators where appropriate, but avoid complexity
-
Example:
int result = (a > b) ? a : b;
-
-
Prefer pre-increment (
++i) over post-increment (i++) in loops where possible for better performance:-
Instead of:
for (int i = 0; i < n; i++)
-
Use:
for (int i = 0; i < n; ++i)
-
-
Use
breakandcontinuewisely-
Avoid unnecessary loop iterations with
continue:for (int i = 0; i < n; ++i) { char* obj = arr[i]; if(!obj) continue; process(obj); }
-
-
Keep functions short and focused
- Each function should ideally perform one task only
- If a function exceeds 100-120 lines, consider refactoring
-
Use meaningful parameter names:
- Bad:
void process(int a, int b) { ... }
- Good:
void process(int width, int height) { ... }