documenting-code.rst 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. Documenting Code
  2. ================
  3. Introduction
  4. ------------
  5. When documenting code for this repository, please follow `Doxygen style <http://www.stack.nl/~dimitri/doxygen/manual/docblocks.html#specialblock>`_. You are doing it by inserting special commands, for instance ``@param``, into standard comments blocks like for example ``/* @param ratio this is oxygen to air ratio */``.
  6. Doxygen is phrasing the code, extracting the commands together with subsequent text, and building documentation out of it.
  7. Typical comment block, that contains documentation of a function, looks like below.
  8. .. image:: _static/doc-code-documentation-inline.png
  9. :align: center
  10. :alt: Sample inline code documentation
  11. Doxygen supports couple of formatting styles. It also gives you great flexibility on level of details to include in documentation. To get the taste of available features please check data reach and very well organized `Doxygen Manual <http://www.stack.nl/~dimitri/doxygen/manual/index.html>`_.
  12. Why we need it?
  13. ---------------
  14. The purpose of this description is to provide quick summary on documentation style used in `espressif/esp-idf <https://github.com/espressif/esp-idf>`_ repository.
  15. The ultimate goal is to ensure that all the code is consistently documented, so we can use tools like `Sphinx <http://www.sphinx-doc.org/>`_ and `Breathe <https://breathe.readthedocs.io/>`_ to aid preparation and automatic updates of API documentation when the code changes. The above piece of code renders in Sphinx like below:
  16. .. image:: _static/doc-code-documentation-rendered.png
  17. :align: center
  18. :alt: Sample inline code after rendering
  19. Go for it!
  20. ----------
  21. When writing code for this repository, please follow guidelines below.
  22. 1. Document all building blocks of code: functions, structs, typedefs, enums, macros, etc. Provide enough information on purpose, functionality and limitations of documented items, as you would like to see them documented when reading the code by others.
  23. 2. Documentation of function should describe what this function does. If it accepts input parameters and returns some value, all of them should be explained.
  24. 3. Do not add a data type before parameter or any other characters besides spaces. All spaces and line breaks are compressed into a single space. If you like to break a line, then break it twice.
  25. .. image:: _static/doc-code-function.png
  26. :align: center
  27. :alt: Sample function documented inline and after rendering
  28. 4. If function has void input or does not return any value, then skip ``@param`` or ``@return``
  29. .. image:: _static/doc-code-void-function.png
  30. :align: center
  31. :alt: Sample void function documented inline and after rendering
  32. 5. When documenting members of a ``struct``, ``typedef`` or ``enum``, place specific comment like below after each member.
  33. .. image:: _static/doc-code-member.png
  34. :align: center
  35. :alt: Sample of member documentation inline and after rendering
  36. 6. To provide well formatted lists, break the line after command (like ``@return`` in example below).
  37. ::
  38. ...
  39. *
  40. * @return
  41. * - ESP_OK if erase operation was successful
  42. * - ESP_ERR_NVS_INVALID_HANDLE if handle has been closed or is NULL
  43. * - ESP_ERR_NVS_READ_ONLY if handle was opened as read only
  44. * - ESP_ERR_NVS_NOT_FOUND if the requested key doesn't exist
  45. * - other error codes from the underlying storage driver
  46. *
  47. ...
  48. 7. Overview of functionality of documented header file, or group of files that make a library, should be placed in separate ``README.rst`` file.
  49. Go one extra mile
  50. -----------------
  51. There are couple of tips how you can make your documentation even better and more useful to the reader.
  52. Add code snippets to illustrate implementation. To do so, enclose the snippet using ``@code{c}`` and ``@endcode`` commands.
  53. ::
  54. ...
  55. *
  56. * @code{c}
  57. * // Example of using nvs_get_i32:
  58. * int32_t max_buffer_size = 4096; // default value
  59. * esp_err_t err = nvs_get_i32(my_handle, "max_buffer_size", &max_buffer_size);
  60. * assert(err == ESP_OK || err == ESP_ERR_NVS_NOT_FOUND);
  61. * // if ESP_ERR_NVS_NOT_FOUND was returned, max_buffer_size will still
  62. * // have its default value.
  63. * @endcode
  64. *
  65. ...
  66. To highlight some information use command ``@attention`` or ``@note``. Example below also shows how to use a numbered list.
  67. ::
  68. ...
  69. *
  70. * @attention
  71. * 1. This API only impact WIFI_MODE_STA or WIFI_MODE_APSTA mode
  72. * 2. If the ESP32 is connected to an AP, call esp_wifi_disconnect to disconnect.
  73. *
  74. ...
  75. Use markdown to make your documentation even more readable. With markdown you can add headers, links, tables and more.
  76. ::
  77. ...
  78. *
  79. * [ESP32 Technical Reference](http://espressif.com/sites/default/files/documentation/esp32_technical_reference_manual_en.pdf)
  80. *
  81. ...
  82. Wrap up
  83. -------
  84. We love good code that is doing cool things.
  85. We love it even better, if it is well documented, so we can quickly make it run and also do the cool things.