code_style_guidelines.rst 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. .. _doc_code_style_guidelines:
  2. Code style guidelines
  3. =====================
  4. .. highlight:: shell
  5. When contributing to Godot's source code, you will be expected to follow the
  6. style guidelines outlined below. Some of them are checked via the Continuous
  7. Integration process and reviewers will ask you to fix potential issues, so
  8. best setup your system as outlined below to ensure all your commits follow the
  9. guidelines.
  10. C++ and Objective-C
  11. -------------------
  12. There are no written guidelines, but the code style agreed upon by the
  13. developers is enforced via the `clang-format <https://clang.llvm.org/docs/ClangFormat.html>`__
  14. code beautifier, which takes care for you of all our conventions.
  15. To name a few:
  16. - Indentation and alignment are both tab based (respectively one and two tabs)
  17. - One space around math and assignments operators as well as after commas
  18. - Pointer and reference operators are affixed to the variable identifier, not
  19. to the type name
  20. - See further down regarding header includes
  21. The rules used by clang-format are outlined in the
  22. `.clang-format <https://github.com/godotengine/godot/blob/master/.clang-format>`__
  23. file of the Godot repository.
  24. As long as you ensure that your style matches the surrounding code and that you're
  25. not introducing trailing whitespace or space-based indentation, you should be
  26. fine. If you plan to contribute regularly, however, we strongly advise that you
  27. set up clang-format locally to check and automatically fix all your commits.
  28. .. warning:: Godot's code style should *not* be applied to third-party code,
  29. i.e. code that is included in Godot's source tree, but was not written
  30. specifically for our project. Such code usually comes from
  31. different upstream projects with their own style guides (or lack
  32. thereof), and don't want to introduce differences that would make
  33. syncing with upstream repositories harder.
  34. Third-party code is usually included in the ``thirdparty/`` folder
  35. and can thus easily be excluded from formatting scripts. For the
  36. rare cases where a third-party code snippet needs to be included
  37. directly within a Godot file, you can use
  38. ``/* clang-format off */`` and ``/* clang-format on */`` to tell
  39. clang-format to ignore a chunk of code.
  40. .. seealso::
  41. These guidelines only cover code formatting. See :ref:`doc_cpp_usage_guidelines`
  42. for a list of language features that are permitted in pull requests.
  43. Using clang-format locally
  44. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  45. You need to use **clang-format 17** to be compatible with Godot's format. Later versions might
  46. be suitable, but previous versions may not support all used options, or format
  47. some things differently, leading to style issues in pull requests.
  48. .. _doc_code_style_guidelines_pre_commit_hook:
  49. Pre-commit hook
  50. ^^^^^^^^^^^^^^^
  51. For ease of use, we provide hooks for Git with the `pre-commit <https://pre-commit.com/>`__
  52. Python framework that will run clang-format automatically on all your commits with the
  53. correct version of clang-format.
  54. To set up:
  55. ::
  56. pip install pre-commit
  57. pre-commit install
  58. You can also run the hook manually with ``pre-commit run``.
  59. .. note::
  60. Previously, we supplied a hook in the folder ``misc/hooks``. If you copied the
  61. script manually, these hooks should still work, but symlinks will be broken.
  62. If you are using the new system, run ``rm .git/hooks/*`` to remove the old hooks
  63. that are no longer needed.
  64. Installation
  65. ^^^^^^^^^^^^
  66. Here's how to install clang-format:
  67. - Linux: It will usually be available out-of-the-box with the clang toolchain
  68. packaged by your distribution. If your distro version is not the required one,
  69. you can download a pre-compiled version from the
  70. `LLVM website <https://releases.llvm.org/download.html>`__, or if you are on
  71. a Debian derivative, use the `upstream repos <https://apt.llvm.org/>`__.
  72. - macOS and Windows: You can download precompiled binaries from the
  73. `LLVM website <https://releases.llvm.org/download.html>`__. You may need to add
  74. the path to the binary's folder to your system's ``PATH`` environment
  75. variable to be able to call clang-format out of the box.
  76. You then have different possibilities to apply clang-format to your changes:
  77. Manual usage
  78. ^^^^^^^^^^^^
  79. You can apply clang-format manually for one or more files with the following
  80. command:
  81. ::
  82. clang-format -i <path/to/file(s)>
  83. - ``-i`` means that the changes should be written directly to the file (by
  84. default clang-format would only output the fixed version to the terminal).
  85. - The path can point to several files, either one after the other or using
  86. wildcards like in a typical Unix shell. Be careful when globbing so that
  87. you don't run clang-format on compiled objects (.o and .a files) that are
  88. in Godot's tree. So better use ``core/*.{cpp,h}`` than ``core/*``.
  89. IDE plugin
  90. ^^^^^^^^^^
  91. Most IDEs or code editors have beautifier plugins that can be configured to run
  92. clang-format automatically, for example, each time you save a file.
  93. Here is a non-exhaustive list of beautifier plugins for some IDEs:
  94. - Qt Creator: `Beautifier plugin <https://doc.qt.io/qtcreator/creator-beautifier.html>`__
  95. - Visual Studio Code: `Clang-Format <https://marketplace.visualstudio.com/items?itemName=xaver.clang-format>`__
  96. - Visual Studio: `Clang Power Tools 2022 <https://marketplace.visualstudio.com/items?itemName=caphyon.ClangPowerTools2022>`__
  97. - vim: `vim-clang-format <https://github.com/rhysd/vim-clang-format>`__
  98. - CLion: Starting from version ``2019.1``, no plugin is required. Instead, enable
  99. `ClangFormat <https://www.jetbrains.com/help/clion/clangformat-as-alternative-formatter.html#clion-support>`__
  100. (Pull requests are welcome to extend this list with tested plugins.)
  101. .. _doc_code_style_guidelines_header_includes:
  102. Header includes
  103. ~~~~~~~~~~~~~~~
  104. When adding new C++ or Objective-C files or including new headers in existing
  105. ones, the following rules should be followed:
  106. - The first lines in the file should be Godot's copyright header and MIT
  107. license, copy-pasted from another file. Make sure to adjust the filename.
  108. - In a ``.h`` header, include guards should be used with the form
  109. ``FILENAME_H``.
  110. - In a ``.cpp`` file (e.g. ``filename.cpp``), the first include should be the
  111. one where the class is declared (e.g. ``#include "filename.h"``), followed by
  112. an empty line for separation.
  113. - Then come headers from Godot's own code base, included in alphabetical order
  114. (enforced by ``clang-format``) with paths relative to the root folder. Those
  115. includes should be done with quotes, e.g. ``#include "core/object.h"``. The
  116. block of Godot header includes should then be followed by an empty line for
  117. separation.
  118. - Finally, third-party headers (either from ``thirdparty`` or from the system's
  119. include paths) come next and should be included with the < and > symbols, e.g.
  120. ``#include <png.h>``. The block of third-party headers should also be followed
  121. by an empty line for separation.
  122. - Godot and third-party headers should be included in the file that requires
  123. them, i.e. in the `.h` header if used in the declarative code or in the `.cpp`
  124. if used only in the imperative code.
  125. Example:
  126. .. code-block:: cpp
  127. :caption: my_new_file.h
  128. /**************************************************************************/
  129. /* my_new_file.h */
  130. /**************************************************************************/
  131. /* This file is part of: */
  132. /* GODOT ENGINE */
  133. /* https://godotengine.org */
  134. /**************************************************************************/
  135. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  136. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  137. /* */
  138. /* Permission is hereby granted, free of charge, to any person obtaining */
  139. /* a copy of this software and associated documentation files (the */
  140. /* "Software"), to deal in the Software without restriction, including */
  141. /* without limitation the rights to use, copy, modify, merge, publish, */
  142. /* distribute, sublicense, and/or sell copies of the Software, and to */
  143. /* permit persons to whom the Software is furnished to do so, subject to */
  144. /* the following conditions: */
  145. /* */
  146. /* The above copyright notice and this permission notice shall be */
  147. /* included in all copies or substantial portions of the Software. */
  148. /* */
  149. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  150. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  151. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  152. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  153. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  154. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  155. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  156. /**************************************************************************/
  157. #ifndef MY_NEW_FILE_H
  158. #define MY_NEW_FILE_H
  159. #include "core/hash_map.h"
  160. #include "core/list.h"
  161. #include "scene/gui/control.h"
  162. #include <png.h>
  163. ...
  164. #endif // MY_NEW_FILE_H
  165. .. code-block:: cpp
  166. :caption: my_new_file.cpp
  167. /**************************************************************************/
  168. /* my_new_file.cpp */
  169. /**************************************************************************/
  170. /* This file is part of: */
  171. /* GODOT ENGINE */
  172. /* https://godotengine.org */
  173. /**************************************************************************/
  174. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  175. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  176. /* */
  177. /* Permission is hereby granted, free of charge, to any person obtaining */
  178. /* a copy of this software and associated documentation files (the */
  179. /* "Software"), to deal in the Software without restriction, including */
  180. /* without limitation the rights to use, copy, modify, merge, publish, */
  181. /* distribute, sublicense, and/or sell copies of the Software, and to */
  182. /* permit persons to whom the Software is furnished to do so, subject to */
  183. /* the following conditions: */
  184. /* */
  185. /* The above copyright notice and this permission notice shall be */
  186. /* included in all copies or substantial portions of the Software. */
  187. /* */
  188. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  189. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  190. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  191. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  192. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  193. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  194. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  195. /**************************************************************************/
  196. #include "my_new_file.h"
  197. #include "core/math/math_funcs.h"
  198. #include "scene/gui/line_edit.h"
  199. #include <zlib.h>
  200. #include <zstd.h>
  201. Java
  202. ----
  203. Godot's Java code (mostly in ``platform/android``) is also enforced via
  204. ``clang-format``, so see the instructions above to set it up. Keep in mind that
  205. this style guide only applies to code written and maintained by Godot, not
  206. third-party code such as the ``java/src/com/google`` subfolder.
  207. Python
  208. ------
  209. Godot's SCons buildsystem is written in Python, and various scripts included
  210. in the source tree are also using Python.
  211. For those, we use the `Ruff linter and code formatter <https://docs.astral.sh/ruff/>`__.
  212. Using ruff locally
  213. ~~~~~~~~~~~~~~~~~~~
  214. First of all, you will need to install Ruff. Ruff requires Python 3.7+ to run.
  215. Installation
  216. ^^^^^^^^^^^^
  217. Here's how to install ruff:
  218. ::
  219. pip3 install ruff --user
  220. You then have different possibilities to apply ruff to your changes:
  221. Manual usage
  222. ^^^^^^^^^^^^
  223. You can apply ``ruff`` manually to one or more files with the following
  224. command:
  225. ::
  226. ruff -l 120 <path/to/file(s)>
  227. - ``-l 120`` means that the allowed number of characters per line is 120.
  228. This number was agreed upon by the developers.
  229. - The path can point to several files, either one after the other or using
  230. wildcards like in a typical Unix shell.
  231. Pre-commit hook
  232. ~~~~~~~~~~~~~~~
  233. For ease of use, we provide hooks for Git with the `pre-commit <https://pre-commit.com/>`__
  234. Python framework that will run ``ruff`` automatically on all your commits with the
  235. correct version of ``ruff``.
  236. To set up:
  237. ::
  238. pip install pre-commit
  239. pre-commit install
  240. You can also run the hook manually with ``pre-commit run``.
  241. .. note::
  242. Previously, we supplied a hook in the folder ``misc/hooks``. If you copied the
  243. script manually, these hooks should still work, but symlinks will be broken.
  244. If you are using the new system, run ``rm .git/hooks/*`` to remove the old hooks
  245. that are no longer needed.
  246. Editor integration
  247. ^^^^^^^^^^^^^^^^^^
  248. Many IDEs or code editors have beautifier plugins that can be configured to run
  249. ruff automatically, for example, each time you save a file. For details, you can
  250. check `Ruff Integrations <https://docs.astral.sh/ruff/integrations/>`__.
  251. Comment style guide
  252. -------------------
  253. This comment style guide applies to all programming languages used within
  254. Godot's codebase.
  255. - Begin comments with a space character to distinguish them from disabled code.
  256. - Use sentence case for comments. Begin comments with an uppercase character and
  257. always end them with a period.
  258. - Reference variable/function names and values using backticks.
  259. - Wrap comments to ~100 characters.
  260. - You can use ``TODO:``, ``FIXME:``, ``NOTE:``, ``WARNING:``, or ``HACK:`` as admonitions
  261. when needed.
  262. **Example:**
  263. .. code-block:: cpp
  264. // Compute the first 10,000 decimals of Pi.
  265. // FIXME: Don't crash when computing the 1,337th decimal due to `increment`
  266. // being negative.
  267. Don't repeat what the code says in a comment. Explain the *why* rather than *how*.
  268. **Bad:**
  269. .. code-block:: cpp
  270. // Draw loading screen.
  271. draw_load_screen();
  272. You can use Javadoc-style comments above function or macro definitions. It's
  273. recommended to use Javadoc-style comments *only* for methods which are not
  274. exposed to scripting. This is because exposed methods should be documented in
  275. the :ref:`class reference XML <doc_updating_the_class_reference>`
  276. instead.
  277. **Example:**
  278. .. code-block:: cpp
  279. /**
  280. * Returns the number of nodes in the universe.
  281. * This can potentially be a very large number, hence the 64-bit return type.
  282. */
  283. uint64_t Universe::get_node_count() {
  284. // ...
  285. }
  286. For member variables, don't use Javadoc-style comments, but use single-line comments instead:
  287. .. code-block:: cpp
  288. class Universe {
  289. // The cached number of nodes in the universe.
  290. // This value may not always be up-to-date with the current number of nodes
  291. // in the universe.
  292. uint64_t node_count_cached = 0;
  293. };