Computer Science
Grade 9
20 min
Documentation
Documentation
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Define what code documentation is and explain its importance for collaboration and maintenance.
Identify and correctly use single-line and multi-line comments in a programming language.
Write effective comments for variables, functions, and complex code blocks.
Differentiate between helpful and unhelpful (redundant or vague) comments.
Explain the concept of self-documenting code through clear naming conventions.
Describe the purpose of a high-level README file for a project.
Ever tried to read someone else's messy handwriting or follow a recipe with missing steps? 🗺️ Code without notes is just like that, a puzzle without a guide!
In this lesson, you'll learn how to leave clear, helpful notes inside your code. This practice, called documentation...
2
Key Concepts & Vocabulary
TermDefinitionExample
DocumentationThe practice of adding explanatory notes and materials to your code. It's the human-readable information that helps people understand, use, and modify the software.This includes comments inside the code, a README file for the project, and guides on how to use a program.
CommentA programmer-readable note directly inside the source code. The computer ignores comments when running the program.In Python: `# This line calculates the user's final score.`
Single-Line CommentA comment that takes up only one line. It's typically used for short, quick explanations of a specific line of code.In Python, it starts with a `#`. For example: `player_score = 100 # Set initial score`
Multi-Line Comment (Block Comment)A comment that can span across multiple...
3
Core Syntax & Patterns
Single-Line Comment Syntax
In Python: `# text`
In JavaScript/Java/C++: `// text`
Use this for brief notes on a single line. Place it above the line it refers to, or at the end of the line if it's very short.
Multi-Line Comment Syntax
In Python: `""" line 1
line 2 """`
In JavaScript/Java/C++: `/* line 1
line 2 */`
Use this for longer explanations that won't fit on one line. In Python, this is often used for 'docstrings' at the start of a function.
The 'Why, Not What' Rule
Good comments explain the *purpose* or *reasoning* behind the code, not just what the code is literally doing.
Avoid restating the code. Instead of `# Add 1 to score`, write `# Add bonus point for completing the level`.
4 more steps in this tutorial
Sign up free to access the complete tutorial with worked examples and practice.
Sign Up Free to ContinueSample Practice Questions
Challenging
A senior developer says, "Your goal should be to write code that needs no comments at all." How does this statement relate to the tutorial's concepts, and what is a situation where comments are still absolutely necessary?
A.The statement is false; all code needs comments, and self-documenting code is a myth.
B.It aligns with the concept of a README file; comments are only needed for installation instructions.
C.It aligns with the pitfall of commenting bad code; comments are still needed to explain what every single line does.
D.It aligns with the ideal of self-documenting code; however, comments are still essential to explain the 'why' behind non-obvious logic or business rules.
Challenging
A project has excellent, detailed in-code comments explaining every function, but it lacks a README file. Why would a new developer still find it very difficult to start working on this project?
A.Without a README, they won't know the project's overall purpose, how to install dependencies, or how to run the code for the first time.
B.Because a project without a README file will cause a compilation error in most languages.
C.Because in-code comments are usually outdated, so they can't be trusted.
D.Because the in-code comments would be too detailed and confusing for a new person.
Challenging
You see the following code in a game: `p.h = 50 # Set health to 50`. Based on the principles of self-documenting code and the 'Why, Not What' rule, which of these is the best improvement?
A.player.health = 50 # Set player health to the integer value 50
B.player.health = STARTING_HEALTH # Reset health to the default starting value after respawn
C.h = 50 # Set health
D.player_health = 50 # This line changes the player's health
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing Free