code_style_guidelines.rst 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. 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. /**************************************************************************/
  128. /* my_new_file.h */
  129. /**************************************************************************/
  130. /* This file is part of: */
  131. /* GODOT ENGINE */
  132. /* https://godotengine.org */
  133. /**************************************************************************/
  134. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  135. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  136. /* */
  137. /* Permission is hereby granted, free of charge, to any person obtaining */
  138. /* a copy of this software and associated documentation files (the */
  139. /* "Software"), to deal in the Software without restriction, including */
  140. /* without limitation the rights to use, copy, modify, merge, publish, */
  141. /* distribute, sublicense, and/or sell copies of the Software, and to */
  142. /* permit persons to whom the Software is furnished to do so, subject to */
  143. /* the following conditions: */
  144. /* */
  145. /* The above copyright notice and this permission notice shall be */
  146. /* included in all copies or substantial portions of the Software. */
  147. /* */
  148. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  149. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  150. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  151. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  152. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  153. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  154. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  155. /**************************************************************************/
  156. #ifndef MY_NEW_FILE_H
  157. #define MY_NEW_FILE_H
  158. #include "core/hash_map.h"
  159. #include "core/list.h"
  160. #include "scene/gui/control.h"
  161. #include <png.h>
  162. ...
  163. #endif // MY_NEW_FILE_H
  164. .. code-block:: cpp
  165. /**************************************************************************/
  166. /* my_new_file.cpp */
  167. /**************************************************************************/
  168. /* This file is part of: */
  169. /* GODOT ENGINE */
  170. /* https://godotengine.org */
  171. /**************************************************************************/
  172. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  173. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  174. /* */
  175. /* Permission is hereby granted, free of charge, to any person obtaining */
  176. /* a copy of this software and associated documentation files (the */
  177. /* "Software"), to deal in the Software without restriction, including */
  178. /* without limitation the rights to use, copy, modify, merge, publish, */
  179. /* distribute, sublicense, and/or sell copies of the Software, and to */
  180. /* permit persons to whom the Software is furnished to do so, subject to */
  181. /* the following conditions: */
  182. /* */
  183. /* The above copyright notice and this permission notice shall be */
  184. /* included in all copies or substantial portions of the Software. */
  185. /* */
  186. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  187. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  188. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  189. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  190. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  191. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  192. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  193. /**************************************************************************/
  194. #include "my_new_file.h"
  195. #include "core/math/math_funcs.h"
  196. #include "scene/gui/line_edit.h"
  197. #include <zlib.h>
  198. #include <zstd.h>
  199. Java
  200. ----
  201. Godot's Java code (mostly in ``platform/android``) is also enforced via
  202. ``clang-format``, so see the instructions above to set it up. Keep in mind that
  203. this style guide only applies to code written and maintained by Godot, not
  204. third-party code such as the ``java/src/com/google`` subfolder.
  205. Python
  206. ------
  207. Godot's SCons buildsystem is written in Python, and various scripts included
  208. in the source tree are also using Python.
  209. For those, we use the `Ruff linter and code formatter <https://docs.astral.sh/ruff/>`__.
  210. Using ruff locally
  211. ~~~~~~~~~~~~~~~~~~~
  212. First of all, you will need to install Ruff. Ruff requires Python 3.7+ to run.
  213. Installation
  214. ^^^^^^^^^^^^
  215. Here's how to install ruff:
  216. ::
  217. pip3 install ruff --user
  218. You then have different possibilities to apply ruff to your changes:
  219. Manual usage
  220. ^^^^^^^^^^^^
  221. You can apply ``ruff`` manually to one or more files with the following
  222. command:
  223. ::
  224. ruff -l 120 <path/to/file(s)>
  225. - ``-l 120`` means that the allowed number of characters per line is 120.
  226. This number was agreed upon by the developers.
  227. - The path can point to several files, either one after the other or using
  228. wildcards like in a typical Unix shell.
  229. Pre-commit hook
  230. ~~~~~~~~~~~~~~~
  231. For ease of use, we provide hooks for Git with the `pre-commit <https://pre-commit.com/>`__
  232. Python framework that will run ``ruff`` automatically on all your commits with the
  233. correct version of ``ruff``.
  234. To set up:
  235. ::
  236. pip install pre-commit
  237. pre-commit install
  238. You can also run the hook manually with ``pre-commit run``.
  239. .. note::
  240. Previously, we supplied a hook in the folder ``misc/hooks``. If you copied the
  241. script manually, these hooks should still work, but symlinks will be broken.
  242. If you are using the new system, run ``rm .git/hooks/*`` to remove the old hooks
  243. that are no longer needed.
  244. Editor integration
  245. ^^^^^^^^^^^^^^^^^^
  246. Many IDEs or code editors have beautifier plugins that can be configured to run
  247. ruff automatically, for example, each time you save a file. For details, you can
  248. check `Ruff Integrations <https://docs.astral.sh/ruff/integrations/>`__.
  249. Comment style guide
  250. -------------------
  251. This comment style guide applies to all programming languages used within
  252. Godot's codebase.
  253. - Begin comments with a space character to distinguish them from disabled code.
  254. - Use sentence case for comments. Begin comments with an uppercase character and
  255. always end them with a period.
  256. - Reference variable/function names and values using backticks.
  257. - Wrap comments to ~100 characters.
  258. - You can use ``TODO:``, ``FIXME:``, ``NOTE:``, or ``HACK:`` as admonitions
  259. when needed.
  260. **Example:**
  261. .. code-block:: cpp
  262. // Compute the first 10,000 decimals of Pi.
  263. // FIXME: Don't crash when computing the 1,337th decimal due to `increment`
  264. // being negative.
  265. Don't repeat what the code says in a comment. Explain the *why* rather than *how*.
  266. **Bad:**
  267. .. code-block:: cpp
  268. // Draw loading screen.
  269. draw_load_screen();
  270. You can use Javadoc-style comments above function or macro definitions. It's
  271. recommended to use Javadoc-style comments *only* for methods which are not
  272. exposed to scripting. This is because exposed methods should be documented in
  273. the :ref:`class reference XML <doc_updating_the_class_reference>`
  274. instead.
  275. **Example:**
  276. .. code-block:: cpp
  277. /**
  278. * Returns the number of nodes in the universe.
  279. * This can potentially be a very large number, hence the 64-bit return type.
  280. */
  281. uint64_t Universe::get_node_count() {
  282. // ...
  283. }
  284. For member variables, don't use Javadoc-style comments but use single-line comments instead:
  285. .. code-block:: cpp
  286. class Universe {
  287. // The cached number of nodes in the universe.
  288. // This value may not always be up-to-date with the current number of nodes
  289. // in the universe.
  290. uint64_t node_count_cached = 0;
  291. };