McTweak.ai

Learning to code through comic adventures!

CHAPTER 10: EDITION 8

Documentation

The art of telling future developers (including yourself) what the heck your code does

Chapter Progress 8/10
TrashyMcTweak
TrashyMcTweak

Look, I've written the most GLORIOUS code ever created. This final project is going to BLOW. PEOPLE'S. MINDS. We're talking innovation that makes quantum computing look like an abacus!

GrumpyMcTweak
GrumpyMcTweak

WHAT IS THIS ABOMINATION?! There's not a SINGLE comment in this ENTIRE codebase! HOW IS ANYONE SUPPOSED TO UNDERSTAND YOUR DERANGED LOGIC?! It's a SECURITY NIGHTMARE!

AllyMcTweak
AllyMcTweak

I hate to agree with Grumpy, but he's right. This code is completely undocumented. No comments, no README file... it's like you're actively trying to confuse everyone.

TrashyMcTweak
TrashyMcTweak

Comments? README? Who needs 'em! Good code is self-explanatory. If you can't understand my artistic vision by just LOOKING at it, maybe you should find another profession. Like goat herding.

CodyMcTweak
CodyMcTweak

Um, I'm having trouble figuring out what this function is supposed to do... and why it's named "destroyTheWorld"?

FattyMcTweak
FattyMcTweak

This is precisely why my premium clients pay extra for comprehensive documentation. It's like buying a luxury car – you expect the manual to be printed on fancy paper with diagrams, not scribbled on a napkin.

AshleyMcTweak
AshleyMcTweak

Legally speaking, undocumented code is a liability time bomb. When you inevitably get hit by a bus – or more likely, when Snowzie finally snaps after you steal her treats one too many times – no one will be able to maintain this.

TrashyMcTweak
TrashyMcTweak

Fine! FINE! I'll add some stupid comments. But they'll be ARTISTIC comments. VISIONARY comments. Comments that will make Shakespeare look like he was just keysmashing!

GarbageMcTweak
GarbageMcTweak

Code tells you how. Comments tell you why.

AllyMcTweak
AllyMcTweak

Exactly. And let's not forget the README file. It's like the cover letter for your code – it explains what your project does, how to install it, how to use it, and any other information someone might need.

TrashyMcTweak
TrashyMcTweak

README? More like DON'T-README, am I right? *crickets* ...Okay, FINE. Let's get this documentation party started. But I'm warning you – my genius doesn't conform to conventional commenting standards!

Why Document Your Code?

For Others

Other developers need to understand your code to work with it effectively. Good documentation makes collaboration possible and reduces the "bus factor" (what happens if you get hit by a bus?).

For Yourself

Future you will thank present you. Even after a few weeks, you'll forget why you wrote code a certain way. Documentation is a gift to your future self.

For The Project

Documentation increases a project's lifespan and usability. Undocumented code becomes "legacy code" much faster and is more likely to be abandoned.

AllyMcTweak
AllyMcTweak

Documentation isn't just busy work – it's an essential part of professional development. Even the best code is useless if no one can understand or maintain it.

FattyMcTweak
FattyMcTweak

Think of it as insurance for your code's value. The most expensive code is the code that works perfectly but that nobody can understand how to use or modify.

Code Comments: Tell a Story

TrashyMcTweak
TrashyMcTweak

Alright, I'm ready to grace this code with my commentary brilliance. How do you want me to comment? Single-line? Multi-line? ASCII art? Interpretive emoji dance?

AshleyMcTweak
AshleyMcTweak

Let's start with the basics. There are different types of comments for different purposes.

Types of Comments

Single-line
Multi-line
JSDoc/Documentation
TODOs & Flags
// Single-line comments are great for brief explanations
function calculateScore(clicks, multiplier) {
  // Add bonus points for combos over 10
  let bonus = clicks > 10 ? 50 : 0;
  
  return clicks * multiplier + bonus;
}

Single-line comments are created with // Your comment and are ideal for short explanations. Use them to explain why a piece of code exists rather than what it does (which should be obvious from the code itself).

/*
 * Multi-line comments are useful for longer explanations
 * that need multiple paragraphs or when you want to
 * comment out large blocks of code temporarily.
 * 
 * They're also good for section headers to divide your code
 * into logical segments.
 */

/* -----------------------------------
 * GAME STATE MANAGEMENT
 * ----------------------------------- */
class GameState {
  // Class implementation...
}

Multi-line comments start with /* and end with */. They're useful for more detailed explanations, temporary code removal, and creating visual breaks between sections.

/**
 * Calculates the player's score based on clicks and multiplier
 * 
 * @param {number} clicks - The number of successful clicks
 * @param {number} multiplier - The current score multiplier
 * @returns {number} The calculated score with any applicable bonuses
 * @example
 * // Returns 150
 * calculateScore(5, 30)
 */
function calculateScore(clicks, multiplier) {
  let bonus = clicks > 10 ? 50 : 0;
  return clicks * multiplier + bonus;
}

Documentation comments (like JSDoc for JavaScript) provide structured information about functions, classes, and methods. They support special tags that can be processed by documentation tools to generate reference documentation.

Common tags include @param, @returns, @example, @throws, and @deprecated.

// TODO: Add support for power-up items
function processClickEvent(event) {
  // FIXME: Currently doesn't work on mobile devices
  updateScore();
  
  // HACK: Temporary workaround until we fix the animation glitch
  setTimeout(resetAnimation, 100);
  
  // NOTE: This approach might cause performance issues with many objects
  updateAllGameObjects();
}

Special comment flags help highlight areas that need attention. Common flags include:

  • TODO: Features or changes to implement later
  • FIXME: Known bugs that need fixing
  • HACK: A workaround that should be replaced with a better solution
  • NOTE: Important information for other developers
  • BUG: A known issue without a current solution

Many code editors recognize these flags and provide special highlighting or task lists for them.

Before & After: The Power of Good Comments

VS

Before: Uncommented

function process(data) {
  const threshold = 0.85;
  let results = [];
  
  for (let i = 0; i < data.length; i++) {
    if (data[i].value > threshold) {
      results.push({
        id: data[i].id,
        score: (data[i].value - threshold) * 100,
        priority: data[i].value > 0.95 ? "high" : "medium"
      });
    }
  }
  
  return results.sort((a, b) => b.score - a.score);
}

What does this function do? Why is 0.85 important? What do the results mean? Without comments, these questions are left unanswered.

After: Well-Commented

/**
 * Processes game data to identify high-performing elements
 * that exceed our minimum quality threshold
 * 
 * @param {Array} data - Array of game elements with .value properties
 * @returns {Array} Sorted results with calculated scores and priorities
 */
function process(data) {
  // Minimum quality threshold for elements (85%)
  const threshold = 0.85;
  let results = [];
  
  // Process each element to calculate scores
  for (let i = 0; i < data.length; i++) {
    // Only include elements above our quality threshold
    if (data[i].value > threshold) {
      results.push({
        id: data[i].id,
        // Convert to percentage points above threshold
        score: (data[i].value - threshold) * 100,
        // Elements above 95% get high priority
        priority: data[i].value > 0.95 ? "high" : "medium"
      });
    }
  }
  
  // Sort by score in descending order
  return results.sort((a, b) => b.score - a.score);
}

Now it's clear what the function does, why certain values are used, and how the results are calculated. Future developers can understand and modify this code more easily.

GrumpyMcTweak
GrumpyMcTweak

DON'T OVER-COMMENT! Comments that just repeat what the code obviously does are WORSE than no comments at all! They create MAINTENANCE NIGHTMARES!

GarbageMcTweak
GarbageMcTweak

Good comments explain "why," not "what." If your code needs comments to explain what it does, rewrite the code.

Comment Best Practices

DO

  • Explain why something is done, not just what is done
  • Document unexpected behavior or edge cases
  • Use documentation comments for public APIs and functions
  • Explain complex algorithms or business logic
  • Update comments when you change code
  • Include references to external resources or issue trackers

DON'T

  • State the obvious (e.g., // increment counter for counter++)
  • Let comments become outdated as code changes
  • Write comments as a substitute for clear code
  • Include sensitive information (passwords, API keys)
  • Write commenting for the sake of meeting a "minimum comment" requirement
  • Leave "commented-out" code blocks in production code
TrashyMcTweak
TrashyMcTweak

Fine, I've added some perfect comments to my code. Happy now? But just so you know, my variable names are so genius they're practically self-documenting!

AllyMcTweak
AllyMcTweak

Oh really? Let me see... *looks at screen* You have variables named 'x', 'temp', 'stuff', and my personal favorite, 'magicNumber42'...

TrashyMcTweak
TrashyMcTweak

It's called being EFFICIENT! Why waste perfectly good bytes on long, descriptive variable names?

GarbageMcTweak
GarbageMcTweak

Modern compilers optimize code size. Descriptive variable names cost nothing in production but save hours of debugging time.

README Files: Your Project's Front Door

AshleyMcTweak
AshleyMcTweak

Now that we've fixed the comments, let's talk about the README file. It's the first thing people see when they visit your project, and it's essential for explaining what your project does and how to use it.

CodyMcTweak
CodyMcTweak

I always get confused about what to include in a README... is there a standard format?

README.md Template

Structure
Example
Markdown Tips

# Project Title

A short, memorable name for your project.

## Description

A clear, concise explanation of what your project does and why it's useful.

## Features

List the key features of your project.

## Installation

Step-by-step instructions on how to install and set up your project.

## Usage

Examples showing how to use your project, with code snippets and screenshots.

## Technologies

List the technologies, frameworks, and libraries used in your project.

## License

Information about the license your project is under.

## Contributing

(Optional) Guidelines for how others can contribute to your project.

## Credits

(Optional) Acknowledgments for any resources or people who helped with the project.

## Contact

(Optional) Information about how to reach you for questions or feedback.

# Clicker Hero

A fast-paced clicker game where players earn points by clicking targets that appear and disappear at increasing speeds.

## Features

- Dynamic targets that appear in random locations
- Progressive difficulty with faster targets
- Score tracking with high score display
- Sound effects for successful clicks and game events
- Multiple game states (start, playing, game over)

## Installation

1. Clone the repository:
git clone https://github.com/yourusername/clicker-hero.git

2. Open the project folder
3. Launch `index.html` in your web browser

## Usage

1. Click "Start Game" to begin
2. Click on targets as they appear to earn points
3. Try to click as many targets as possible before the timer runs out
4. Your high score will be saved locally

## Technologies

- HTML5
- CSS3
- JavaScript
- Web Audio API for sound effects
- Local Storage for saving high scores

## License

This project is licensed under the MIT License - see the LICENSE.md file for details.

## Credits

- Sound effects from [Freesound.org](https://freesound.org)
- Font "Press Start 2P" from Google Fonts
- Created as final project for McTweak.ai Web Development Course

Markdown Basics

# Heading 1
## Heading 2
### Heading 3

**Bold text**
*Italic text*

[Link text](https://example.com)

![Alt text for image](image.jpg)

- Bullet point 1
- Bullet point 2
  - Nested bullet

1. Numbered item 1
2. Numbered item 2

```javascript
// Code block
function example() {
  return "Hello World";
}
```

`Inline code`

--- 

> Blockquote text

Markdown Rendering

Heading 1

Heading 2

Heading 3

Bold text
Italic text

Link text

[Image would display here]

  • Bullet point 1
  • Bullet point 2
    • Nested bullet
  1. Numbered item 1
  2. Numbered item 2
// Code block
function example() {
  return "Hello World";
}

This is Inline code within text


Blockquote text
TrashyMcTweak
TrashyMcTweak

README files are just bureaucracy! If people can't figure out how my brilliant code works by simply FEELING its energy, they don't deserve to use it!

FattyMcTweak
FattyMcTweak

Actually, a well-crafted README increases a project's perceived value by up to 83%. My premium clients specifically request extensive documentation. It's like the difference between a luxury car manual and a sticky note that says "pedal make go."

README Best Practices

Make It Visual

  • Include screenshots of your project in action
  • Add GIFs to demonstrate features or workflows
  • Use tables to organize complex information
  • Add badges showing build status, version, etc.

Example badges:

v1.0.0 HTML5 CSS3 JavaScript

Make It Accessible

  • Start with a table of contents for longer READMEs
  • Use clear hierarchical headings
  • Write in plain language, avoid jargon
  • Provide complete code examples
  • Proofread for spelling and grammar errors
CodyMcTweak
CodyMcTweak

I found that having a good README has helped me a lot when I come back to my old projects. I can't tell you how many times I've opened something I made months ago and had no idea how to run it...

AllyMcTweak
AllyMcTweak

Exactly! Documentation isn't just for others—it's for your future self. Think of it as a time capsule message to future-you who has forgotten all the details of this project.

Activity: Create Your README

AshleyMcTweak
AshleyMcTweak

For your final project, you'll need to create a comprehensive README file. Let's practice by making one for the project you've been working on throughout this course.

Interactive README Creator

README.md
FattyMcTweak
FattyMcTweak

Don't forget to preview your README! Markdown looks very different when rendered. You want the luxury experience, not the economy version.

GrumpyMcTweak
GrumpyMcTweak

And SPELL CHECK IT! Nothing says "MY CODE IS PROBABLY JUST AS SLOPPY AS THIS DOCUMENTATION" like a README full of typos and grammatical ATROCITIES!

Wrapping Up: Documentation is a Gift

Key Takeaways

Code Comments

Explain the "why" behind your code, not just what it does. Use comments to clarify complex logic and document non-obvious decisions.

README Files

Create a comprehensive guide that helps others understand your project, how to install it, and how to use it effectively.

Future-Proofing

Good documentation makes your project maintainable over time, both for yourself and for others who may work on it.

TrashyMcTweak
TrashyMcTweak

FINE, I've added comments to my code AND created a README. Are you all happy now? Can I go back to creating digital masterpieces that will revolutionize computing as we know it?

AllyMcTweak
AllyMcTweak

Let me check... *reviews code and README* This is... actually pretty good! Your comments are helpful, and the README covers everything a user would need to know.

CodyMcTweak
CodyMcTweak

Wow, I can actually understand what the code is doing now! And look, the README even has screenshots and examples!

FattyMcTweak
FattyMcTweak

Almost premium quality. I might only charge a 15% consulting fee if I were to improve it further.

TrashyMcTweak
TrashyMcTweak

You know what? I hate to admit it, but it does feel good having everything documented. Now people can truly appreciate my genius instead of being confused by it!

AshleyMcTweak
AshleyMcTweak

I think we're ready for final approval. Should we call Snowzie in to review?

SnowzieMcTweak
SnowzieMcTweak

*excitedly woofs and spins in a circle before pawing approvingly at the screen*

CODE IS COMMITTED!

Next Steps

Coming Up Next

In the next episode, we'll focus on preparing for your final project presentation. You'll learn how to showcase your work effectively and demonstrate both its functionality and your technical knowledge.

Chapter 10, Episode 9: Presentation Prep