Docstring

From GM-RKB
Jump to navigation Jump to search

A Docstring is a string literal specified in source code that is used to document a specific segment of code.

  • Context:
    • It can (typically) include a description of the function/module/class/script, its parameters, return values, and any exceptions raised.
    • It can (often) be accessed programmatically at runtime, enabling dynamic generation of documentation or usage guides.
    • It can be used to document Functions, Classes, Modules, or even entire Scripts.
    • It can serve as an integral part of Automatic Documentation Generation tools.
    • It can vary in syntax and usage across different Programming Languages that support docstrings.
    • It can follow specific conventions within a programming language to enhance readability and utility.
    • It can be enclosed in triple quotes in languages like Python to denote multiline string literals for comprehensive documentation.
    • It can be used in some languages like Python and Haskell as a form of Test-Driven Development, where tests are included within the docstring.
    • It can be part of the language's official style guide, as in PEP 257 for Python.
    • ...
  • Example(s):
    • Use the Triple Quote Approach for multiline docstrings, such as:
      • A Python Docstring, like: python def my_function(): """This is a docstring describing the function.""" pass.
      • A Julia Docstring, like: julia """ This is a docstring describing the function. “"" function myFunction() end.
      • ...
    • Use the Comment-Based Approach for single or multiline docstrings in languages without triple quotes, such as:
      • A Haskell Docstring, like: haskell {-| This is a docstring describing the function. -} myFunction = ... .
      • ...
    • ...
  • Counter-Example(s):
    • A comment using // or /* */ in C or C++, which is not retained at runtime.
    • An annotation in Java, which is used for metadata but not for documentation within the source code.
  • See: Comment (Computer Programming), Docblock, Automatic Documentation Generation, PEP 257.


References

2024