Skip to content

Code Guidelines

Angus Goucher edited this page Sep 7, 2024 · 6 revisions

Added to this list as I (@gusg21) think of them

  • Prefer ::function() over function() for C-style standard library functions (printf, sin/cos/etc)
    • Helps distinguish between member functions and library functions in global namespace
    • Annoying to remember so not the end of the world if forgotten
  • Error constants should be [FILE NAME]_[FUNCTION NAME]_FAIL_[REASON FOR FAILURE] and defined in the header file
    • Try to avoid putting "can't" in the reason for failure because you can't put apostrophes in macro names, try using "bad" instead
  • ONE class or struct per file. Even if you think you'll only ever need to include both at the same time it will bite you. Every time.
  • Prefer pointers over references
    • Easier to read and reason about, fight me.
  • Prefer stack allocation over heap allocation (and by extension arrays over vectors)
  • Always specify your int sizes - uint32_t, int64_t, etc.
  • Never use a signed integer where the value should never be negative - guarantee it!
    • For example in sizes or lengths
  • const your functions whenever you can
  • Prefer to have defaults in the header for member variables and default constructors
    • Use ::init() for actual logic (and make sure it returns uint32_t if it can fail! Or we will stab you.)
  • Pass strings (assuming they're just used as data) use const std::string& otherwise they will be copied 😒 (which is O(n))
  • Never include private variables or methods in structures - that's what classes are for, and if there isn't a difference between class and struct we might as well make a semantic one.
  • Prefer #pragma once over #ifndef XXX_H

Clone this wiki locally