StyleGuide.html 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <!DOCTYPE html> <HTML lang=en> <HEAD> <STYLE>
  2. body { background-color: #EEFFEE; font-size: 1.0rem; font-family: Arial; max-width: 60rem;
  3. color: #000000; margin: 0px;
  4. padding-left: 0px; padding-right: 0px; padding-top: 0px; padding-bottom: 0px; }
  5. H1 { padding-left: 10px; padding-right: 0px; padding-top: 10px; padding-bottom: 10px; font-size: 1.4rem; }
  6. H2 { padding-left: 10px; padding-right: 0px; padding-top: 10px; padding-bottom: 0px; font-size: 1.2rem; }
  7. blockquote {
  8. tab-size: 3rem;
  9. color: #88FF88; background: #000000;
  10. font-size: 0.95rem; font-family: monospace;
  11. padding-left: 5px; padding-right: 5px;
  12. padding-top: 5px; padding-bottom: 5px;
  13. }
  14. P { padding-left: 20px; padding-right: 0px; padding-top: 0px; padding-bottom: 0px; }
  15. IMG { padding-left: 0px; padding-right: 0px; padding-top: 2px; padding-bottom: 0px;
  16. max-width: 100%; }
  17. A { display: inline; border-radius: 4px;
  18. font-size: 1.0rem; font-family: Arial; color: #000044; text-decoration: none;
  19. padding-left: 4px; padding-right: 4px; padding-top: 4px; padding-bottom: 4px; }
  20. A:hover { color: #FFFF00; background: #000044; }
  21. A:active { color: #FFFFFF; background: #444444; }
  22. </STYLE> </HEAD> <BODY>
  23. <IMG SRC="Images/Title.png" ALT="Images/Title.png">
  24. <P>
  25. <A href="Manual.html">Back to main page</A>
  26. </P><P>
  27. </P><H1> Code convention for David Forsgren Piuva's Software Renderer</H1><P>
  28. </P><P>
  29. To keep the style consistent, the style being used in the library is explained in this document.
  30. </P><IMG SRC="Images/Border.png"><P>
  31. Tabs for indentation then spaces for alignment. It's the best of both worlds by both having variable length tabs and correct alignment that works between lines of the same indentation.
  32. </P><IMG SRC="Images/Border.png"><P>
  33. No new line for opening brackets.
  34. Makes the code more compact and decreases the risk of copy-paste errors.
  35. </P><IMG SRC="Images/Border.png"><P>
  36. No hpp extensions, use h for all headers. Could be either way, but this library uses *.h for compact naming, so keep it consistent.
  37. </P><IMG SRC="Images/Border.png"><P>
  38. C-style casting for raw data manipulation and C++-style for high-level classes when possible.
  39. When using assembly intrinsics and raw pointer manipulation to alter the state of bits,
  40. verbose high-level abstractions only make it harder to count CPU cycles in your head.
  41. Always use the tool that makes sense for the problem you're trying to solve.
  42. C++ style is for things that are abstracted on a higher level.
  43. C style is for when a byte is just a byte and you just want to manipulate it in a specific way.
  44. </P><IMG SRC="Images/Border.png"><P>
  45. Follow the most relevant standard without making contemporary assumptions.
  46. For code not intended for a specific system, follow the C++ standard.
  47. For code targeting a certain hardware using intrinsic functions, follow the hardware's standard.
  48. For code targeting a certain operating system, follow the operating system's standard.
  49. </P><IMG SRC="Images/Border.png"><P>
  50. Do not assume that a type has a certain size or format unless it is specified explicitly.
  51. The int type is not always 32 bits, so only use when 16 bits are enough, use int32_t for a signed 32-bit integer.
  52. Fixed integers such as uint8_t, uint16_t, uint32_t, uint64_t, int32_t and int64_t are preferred.
  53. For bit manipulation, use unsigned integers to avoid depending on two's complement.
  54. The char type is usually 8 bits large, but it is not specified by the C++ standard, so use uint8_t instead for buffers and DsrChar for 32-bit Unicode characters.
  55. The float type does not have to be any of the IEEE standards according to the C++ standard, but you can assume properties that are specified in a relevant standard.
  56. std::string is not used, because it has an undefined character encoding, so use dsr::String or dsr::ReadableString with UTF-32 instead.
  57. char* should only be used for constant string literals and interfacing with external libraries.
  58. </P><IMG SRC="Images/Border.png"><P>
  59. The code should work for both little-endian and big-endian, because both still exist.
  60. You may however ignore mixed-endian.
  61. </P><IMG SRC="Images/Border.png"><P>
  62. Do not call member methods with "this" set to null, because that is undefined behavior.
  63. </P><IMG SRC="Images/Border.png"><P>
  64. Leave an empty line at the end of each source document.
  65. Even though the C++ standard tells compilers to ignore line breaks as white space during parsing, white space is still used to separate the tokens that are used.
  66. A future C++ compiler might be designed to allow interactive input directly from the developer and ignore end of file for consistent behavior between command line input and source files.
  67. So without a line break at the end, the last token in a cpp file may be ignored on some compilers.
  68. </P><IMG SRC="Images/Border.png"><P>
  69. Avoid mixing side-effects with expressions for determinism across compilers.
  70. Non-deterministic expressions such as ((x++ - ++x) * x--) should never be used, so use ++ and -- in separate statements.
  71. Side-effects within the same depth of an expressions may be evaluated in any order because it is not specified in C++.
  72. Checking the return value of a function with side-effects is okay, because the side effect always come before returning the result in the called function.
  73. Lazy evaluation such as x != nullptr && foo(x) is okay, because lazy evaluation is well specified as only evaluating the right hand side when needed.
  74. Call chaining such as constructor(args).setSomeValue(value).setSomeOtherValue(value) is okay, because the execution order is explicit from differences in expression depth.
  75. </P><IMG SRC="Images/Border.png"><P>
  76. Use the std library as little as possible.
  77. Each compiler, operating system and standard library implementation has subtle differences in how things work, which can cause programs to break on another computer.
  78. The goal of this framework is to make things more consistent across platforms, so that code that works on one computer is more likely to work on another computer.
  79. </P><IMG SRC="Images/Border.png"><P>
  80. Don't over-use the auto keyword.
  81. Spelling out the type explicitly makes the code easier to read.
  82. </P><IMG SRC="Images/Border.png"><P>
  83. </P>
  84. </BODY> </HTML>