| name | code-commenting |
| description | Comprehensive code commenting methodology for Python projects. Use when user asks to add comments, annotations, or documentation to Python code files. Provides structured approach with module docstrings, class/function documentation, section separators, and inline comments. |
Code Commenting Skill
Add structured, professional comments to Python code files.
Commenting Hierarchy
Add comments in the following hierarchical structure:
1. Module-Level Docstring
Place at the top of the file using triple quotes with separator lines:
"""
============================================================================
Module Name - Brief Description
============================================================================
Detailed description of the module.
This module provides:
- Feature 1
- Feature 2
- Feature 3
Note:
Any important notes for users.
"""
2. Import Section Comments
Group imports by category using separator lines:
import os
import random
from tqdm import tqdm
from myproject.core import Module
3. Section Separators
Use fixed-length separator lines to divide code blocks:
ParamKey = Tuple[int, int]
def main():
pass
4. Class Documentation
class Metrics:
"""
Statistics Collector - Collect metrics during simulation.
Uses Welford's online algorithm for delay statistics.
Attributes:
total_requests: Total number of requests.
collision_failures: Number of collision failures.
Note:
Only data within the statistics window is recorded.
"""
def __init__(self) -> None:
"""Initialize all statistics counters."""
self.total_requests: int = 0
5. Function Documentation
Use detailed docstrings with workflow, parameters, returns, and examples:
def run_simulation(
num_ue: int,
frame: Frame,
) -> Dict:
"""
Run simulation - Brief description.
Detailed explanation of the function's purpose and behavior.
Workflow:
1. Step one
2. Step two
3. Step three
Args:
num_ue: Number of UEs.
Detailed explanation of the parameter.
frame: Frame structure configuration.
- Sub-attribute 1
- Sub-attribute 2
Returns:
Dict: Result dictionary.
- key1: Description
- key2: Description
Example:
>>> result = run_simulation(num_ue=100, frame=Frame())
>>> print(result['success_rate'])
Note:
Any important notes.
"""
6. Step-by-Step Inline Comments
For complex functions, use step-by-step structure:
def complex_function():
total = calculate_total()
instance = create_instance()
for i in range(total):
process_request()
7. Inline Comments
num_slots = 10
if frame_idx < warmup_frames:
continue
delta = value - mean
Formatting Guidelines
Separator Lengths
-
Major sections (=): 77 characters total
# =============================================================================
-
Minor subsections (-): 77 characters total
# -------------------------------------------------------------------------
Best Practices
- Don't repeat the code - Comments should explain "why", not "what"
- Be consistent - Use the same commenting style throughout the project
- Hierarchical structure - Use
= for major blocks, - for sub-blocks
- Step numbering - Use Step X.Y numbering for complex workflows
- Type annotations - Add type explanations for important variables
- Paper references - Note formula numbers when code implements paper equations
Workflow
- Read the file - First read the entire file to understand its structure
- Add module docstring - Add module-level documentation
- Organize imports - Group imports with separator lines
- Document classes - Add docstrings to each class
- Document functions - Add detailed docstrings to each function
- Add section separators - Divide code blocks with separator lines
- Add inline comments - Add inline comments for complex logic
- Review - Ensure comments are complete and consistent
Template Reference
For quick-reference templates, see references/templates.md.
Example: Complete File Structure
"""
============================================================================
Module Name - Brief Description
============================================================================
Module description.
"""
import os
from project import Module
MAX_VALUE = 100
class MyClass:
"""Class description."""
pass
def main():
"""Main function."""
pass
__all__ = [, ]