StyleGuide.txt 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. Code convention:
  2. 1. Use common sense!
  3. If it looks wrong to humans then it's wrong.
  4. Don't defeat the purpose of the rule by taking it too far.
  5. 2. Don't use iterators when there is any other way to accomplish the task.
  6. You can't write efficient algorithms without knowing the data structures.
  7. 3. Tabs for indentation then spaces for alignment.
  8. It's the best of both worlds by both having variable length tabs
  9. and correct alignment that works between lines of the same indentation.
  10. 3.1. Do not use multiple spaces as a replacement for tabs in indentation.
  11. I don't care if you set your editor to 2, 4 or 8 columns per tab
  12. and neither should you care about my preferences.
  13. 3.2. Do not use a tab as a replacement for multiple spaces in alignment.
  14. The number of leading tabs should be equal to the indentation depth.
  15. This way we can prove mathematically that blocks of code in the same
  16. indentation will always keep the same relative alignment.
  17. 3.3. Do not use tabs after spaces.
  18. First tabs for indentation, then spaces for alignment.
  19. 3.4. Do not try to align between different indentation depths.
  20. Only align within the same depth so that it works for all tab lengths.
  21. Example using "--->" for tabs and "." for spaces:
  22. int foo(int x, int y) [
  23. --->if (superLongExpression(x) &&
  24. --->....superLongExpression(y)) {
  25. --->--->bar(x + y);
  26. --->}
  27. }
  28. If a reader uses 8 spaces per tab then superLongExpression is still aligned:
  29. int foo(int x, int y) [
  30. ------->if (superLongExpression(x) &&
  31. ------->....superLongExpression(y)) {
  32. ------->------->bar(x + y);
  33. ------->}
  34. }
  35. 4. No dangling else, use explicit {} for safety.
  36. Otherwise someone might add an extra statement and get random crashes.
  37. 5. No hpp extensions, use h for all headers.
  38. Could be either way, but this library uses *.h for compact naming, so keep it consistent.
  39. 6. C-style casting for raw data manipulation and C++-style for high-level classes.
  40. When using assembly intrinsics and raw pointer manipulation to alter the state of bits,
  41. verbose high-level abstractions only make it harder to count CPU cycles in your head.
  42. Always use the tool that makes sense for the problem you're trying to solve.
  43. C++ style is for things that are abstracted on a higher level.
  44. C style is for when a byte is just a byte and you just want to manipulate it in a specific way.
  45. 7. Don't call member methods with "this" set to nullptr.
  46. This would be undefined behaviour and may randomly crash.
  47. Use global functions instead. They allow checking pointers for null
  48. because they are explicit arguments declared by the programmer.
  49. 8. Avoid using STD/STL directly in SDK examples.
  50. Exposing types from the standard library should be done using an alias or wrapper in the dsr namespace.
  51. This allow replacing the standard library without breaking backward compatibility.
  52. The C++ standard have broken backward compatibility before and it can happen again.
  53. 9. Don't abuse the auto keyword everywhere just to make it look more "modern".
  54. Explicit type safety is what makes compiled languages safer than scripting.
  55. 10. No new line for opening brackets.
  56. Makes the code more compact and decreases the risk of copy-paste errors.
  57. 11. Don't fix the style of someone else's code if you can easily read it.
  58. Being pedantic can become an addiction consuming all your time.
  59. Fixing actual bugs and port to new systems is much more appreciated than
  60. causing version conflicts with others.
  61. 12. Don't change things that you don't know how to test.
  62. * Not doing enough regression tests will eventually create a mine-field of untested functions.
  63. Regression testing will only slow down the amount of new bugs, unless you keep adding more regression tests.
  64. * Manual testing only finds bugs in code that's actually called by the program.
  65. Two bugs may cancel each other out, which is common with negation and coordinate systems.
  66. * The larger the input is relative to the output, the lower coverage you will get from each test.
  67. If your input is a video and the result is a boolean, any implementation has a 50% chance to pass the test on pure luck.
  68. If your input is a boolean and the result is a video, you just try both cases and have full coverage.