A 2D tile-based sandbox game.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

conventions.md 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # Conventions
  2. None of these things are absolute; there's always room for special cases with
  3. special considerations. However, these coding conventions should describe
  4. how this code base generally looks and works.
  5. ## Superficial
  6. * Files are indented using tabs.
  7. * Opening curly braces go at the end of the line.
  8. * Never align, just create new indentation levels; for example, a multi-line
  9. function call should look like this:
  10. ``` C++
  11. SomeNamespace::someLongFunctionName(
  12. SomePossiblyVeryLongExpression(),
  13. 10, 20);
  14. ```
  15. * There's a "soft" line length limit of 80 columns; lines can be longer, but
  16. not significantly longer.
  17. * Multi-line function parameter lists are indented twice, to visually separate
  18. them from the body:
  19. ``` C++
  20. void someLongFunctionName(
  21. int firstArgument,
  22. int secondArgument) {
  23. whatever;
  24. }
  25. ```
  26. ## Names
  27. * Classes and structs are PascalCase.
  28. * Methods and free functions are camelCase.
  29. * Local variables are camelCase.
  30. * Class member variables are camelCase\_ (with the trailing underscore).
  31. * Struct member variables are camelCase (without the trailing underscore).
  32. * Constants are UPPER\_SNAKE\_CASE (no trailing underscore) everywhere.
  33. ## Structure
  34. * Classes are structured with public first, then protected, then private.
  35. * Each section (public, protected, private) of a class starts with the member functions,
  36. then the static member functions, then the member variables.
  37. * Structs don't contain private or protected fields.
  38. * Structs starts with the member variables, then member functions, then static member functions.
  39. * Headers are named `*.h`, source files are named `*.cc`.
  40. ## Behaviour
  41. * In general, use `unique_ptr` and `make_unique` for owning pointers.
  42. * References and raw pointers should always be non-owning.
  43. * Only use `shared_ptr` when you can't get away from shared ownership.
  44. * In the case of cycles, try to make ownership hierarchical; e.g the parent
  45. has a `unique_ptr` to the child, while the child only has a reference
  46. or raw pointer to the parent.
  47. * When the parent and child are in different headers, the child's header should
  48. forward declare the parent, while the parent's header can include the child's
  49. header. Cyclic includes are evil.
  50. * In source files which act as the implementation of a header, the very first
  51. substantial line should include the corresponding header.
  52. * All headers should have a corresponding source file, even if that source file
  53. is empty except for the include. This is to avoid include ordering issues.
  54. ## Rationale
  55. While a lot of this is just because we need to standardize on _something_,
  56. some of the points have pretty good reasons.
  57. * I view structs as "mainly data"; the reason you have a struct is to conveniently
  58. put different variables together. Classes, on the other hand, are "mainly behaviour",
  59. so their member variables (to the degree that they have public members at all)
  60. should be de-emphasized.
  61. * Classes should start with their public members because that's the only thing
  62. other components care about. It's odd to hide away the public interface behind
  63. a wall of implementation details.