Documentation and Code Readability
Students will learn the importance of documentation in improving the usability of a code library.
About This Topic
Code is written once but read many times, by the original author returning after a break, by collaborators, and by future maintainers who may have no prior context. Documentation, including inline comments, docstrings, and clear variable names, bridges the gap between what code does mechanically and why it exists. For 9th graders, this is often the first time they encounter code as a form of professional communication, not just a set of instructions for a computer.
In US K-12 computer science, CSTA 3A-AP-17 and 3A-AP-18 ask students to document design decisions and make code accessible to others. This standard reflects real professional practice: in industry, undocumented code is a liability because it cannot be safely changed by anyone other than its original author. Students who internalize documentation as part of the coding process, rather than an afterthought, build a habit that makes every codebase they touch more maintainable.
Active learning is effective here because readability is inherently social. What seems clear to the author often requires effort from a reader. Peer review activities where students evaluate each other's documentation reveal the gap between perceived and actual clarity, creating specific and actionable feedback that self-assessment rarely produces.
Key Questions
- Explain how documentation improves the usability of a code library.
- Construct effective comments and docstrings for a given function.
- Evaluate the readability of code with and without proper documentation.
Learning Objectives
- Explain how well-written documentation enhances the usability and maintainability of a code library.
- Construct clear and concise comments and docstrings for a given Python function, adhering to established conventions.
- Compare and contrast the readability and understandability of code snippets with and without adequate documentation.
- Evaluate the effectiveness of different documentation strategies in making code accessible to other programmers.
Before You Start
Why: Students need a basic understanding of functions, variables, and control flow to comprehend what needs to be documented.
Why: Familiarity with Python syntax is necessary to write and understand code examples, comments, and docstrings.
Key Vocabulary
| Documentation | Written explanations and information about a piece of software or code, intended to help users and developers understand its purpose, usage, and design. |
| Code Readability | The ease with which a human reader can understand the purpose, structure, and operation of source code. |
| Inline Comment | A note embedded directly within the source code, typically used to explain specific lines or blocks of code. |
| Docstring | A string literal that occurs as the first statement in a module, function, class, or method definition, used to document that object. |
| Maintainability | The ease with which software can be modified to correct defects, improve performance, or adapt to a changed environment. |
Watch Out for These Misconceptions
Common MisconceptionMore comments always means better documentation.
What to Teach Instead
Comments that restate what the code obviously does add noise without adding clarity. A comment that says 'increment i by 1' next to 'i += 1' is redundant. Good documentation explains why, not what, and is targeted at the parts of the code where intent is not immediately obvious. Peer review quickly surfaces over-commenting.
Common MisconceptionDocumentation is something you add after the code is finished.
What to Teach Instead
Writing documentation alongside code, particularly docstrings before implementing a function, helps clarify the function's expected behavior before coding begins. Students who draft a docstring first frequently write cleaner code because the act of describing the function's purpose exposes design decisions they had not fully thought through.
Common MisconceptionOnly large projects with teams need documentation.
What to Teach Instead
Any code you might return to later, or share with anyone else, benefits from documentation. Students who work on individual projects often find that their own code is incomprehensible a week later without good documentation. Real experience with this during peer review is more persuasive than being told it matters.
Active Learning Ideas
See all activitiesPeer Documentation Review
Students exchange programs with a partner who has not seen the code before. The reader attempts to explain, in plain English, what each function does and why specific decisions were made, using only the code and its documentation. The author notes where the reader's interpretation was wrong and revises the documentation to close the gap.
Rewrite for Readability: Before and After
Provide pairs with an undocumented, poorly named version of a working program. They add docstrings, rename variables, and add comments, then compare their documentation choices with another pair and discuss disagreements about what deserves a comment versus a rename.
Gallery Walk: Documentation Quality Spectrum
Post six code samples ranging from no documentation to over-commented, with good documentation in the middle range. Students rank them from most to least readable, annotating what makes each level more or less useful. Class discussion identifies patterns in what constitutes helpful versus unhelpful documentation.
Think-Pair-Share: Comment or Rename?
Present ten code snippets, each with a clarity problem. For each, students decide whether the fix is a comment explaining the logic or a variable/function rename that makes the logic self-evident. Pairs compare choices and discuss the principle behind each decision.
Real-World Connections
- Software developers at Google use extensive documentation for open-source libraries like TensorFlow, enabling millions of researchers and engineers worldwide to build AI applications.
- Game developers on teams creating titles like 'Fortnite' rely on clear code comments and documentation to collaborate effectively, ensuring that new features can be integrated without breaking existing gameplay.
- Open-source projects on platforms like GitHub, such as the Python language interpreter itself, depend heavily on community contributions to documentation, making the software accessible to a global audience of programmers.
Assessment Ideas
Provide students with two versions of a short Python function: one with minimal comments and one with comprehensive docstrings and inline comments. Ask students to read both and then answer: 'Which version is easier to understand and why? List three specific improvements made by the documentation in the second version.'
Present students with a function and ask them to write a docstring that explains its purpose, parameters, and return value. Then, have them add at least two inline comments to clarify complex logic within the function body.
Facilitate a class discussion using the prompt: 'Imagine you are taking over a project from a developer who left the company without any documentation. What are the biggest challenges you would face, and how could good documentation have prevented them?'
Frequently Asked Questions
What is the difference between a comment and a docstring?
What should go in a docstring?
How does active peer review improve documentation quality?
Do professional programmers actually write documentation?
More in Programming with Purpose
Data Types and Variables
Students will learn to use different data types and variables to store and manipulate information in a program.
2 methodologies
Conditional Statements (If/Else)
Students will use conditional statements to control the execution flow of a program based on specific criteria.
2 methodologies
Looping Constructs (For/While)
Students will implement loops to repeat blocks of code, improving efficiency and reducing redundancy.
2 methodologies
Introduction to Functions
Students will design reusable code blocks to improve readability and maintainability.
2 methodologies
Function Design and Reusability
Students will focus on designing functions that are truly reusable across different projects.
2 methodologies
Testing Functions with Inputs
Students will learn to test functions with various inputs to ensure they produce expected outputs.
2 methodologies