More documentation
In-code documentation
In-code documentation makes code more understandable and explains decisions we made. When not to use in-code documentation:
- When the code is self-explanatory
- To replace good variable/function names
- To replace version control
- To keep old (zombie) code around by commenting it out
Readable code vs commented code
vsgood comments
Consider these two comments:
Comment A
Comment B Which of these comments is more useful? Can you explain why?What are “docstrings” and how can they be useful?
Here is the function fahrenheit_to_celsius which converts temperature in Fahrenheit to Celsius.
The first set of examples uses regular comments:
# This function converts a temperature in Fahrenheit to Celsius.
def fahrenheit_to_celsius(temp_f: float) -> float:
temp_c = (temp_f - 32.0) * (5.0/9.0)
return temp_c
The second set uses docstrings or sim
def fahrenheit_to_celsius(temp_f: float) -> float:
"""
Converts a temperature in Fahrenheit to Celsius.
Parameters
----------
temp_f : float
The temperature in Fahrenheit.
Returns
-------
float
The temperature in Celsius.
"""
temp_c = (temp_f - 32.0) * (5.0/9.0)
return temp_c
Docstrings can do a bit more than just comments:
-
Tools can generate help text automatically from the docstrings.
-
Tools can generate documentation pages automatically from code.
It is common to write docstrings for functions, classes, and modules. Good docstrings describe:
- What the function does.
- What goes in (including the type of the input variables).
- What goes out (including the return type).
Naming is documentation: giving explicit, descriptive names to your code segments (functions, classes, variables) already provides very useful and important documentation. In practice you will find that for simple functions it is unnecessary to add a docstring when the function name and variable names already give enough information.