Skip to content

Chapter 2 · Basic Conversations and Code Generation ​

Chapter goals: Master the use of the interactive REPL, learn to ask effective questions to generate high-quality code, and understand context comprehension capabilities.

2.1 Interactive REPL Basics ​

After starting claude in a project directory, you enter an interactive conversation environment:

text
╭───────────────────────────────────╮
│ Welcome to Claude Code!           │
│                                   │
│ /help for help                    │
│ Type your request and press Enter │
╰───────────────────────────────────╯

>

Common Shortcuts and Commands ​

Shortcut/CommandFunction
EnterSend message
Shift+TabMulti-line input mode
Ctrl+CCancel current operation or exit
/clearClear conversation history
/compactCompress context (saves tokens on long sessions)
/helpView help
/costCheck current session token usage
/exitExit

2.2 Direct Questions and Code Generation ​

Claude Code understands natural language descriptions and generates code:

text
> Write a Python function that parses a CSV file and returns a list of unique values from a specified column

Output example:

python
import csv
from typing import Any


def get_unique_column_values(filepath: str, column_name: str) -> list[Any]:
    """Read a CSV file and return a list of unique values from the specified column."""
    values = set()
    with open(filepath, newline='', encoding='utf-8') as f:
        reader = csv.DictReader(f)
        for row in reader:
            if column_name in row:
                values.add(row[column_name])
    return sorted(values)

Questioning Tips

  • Be specific: "Write a function that parses CSV and returns unique column values" works better than "help me process CSV"
  • Provide constraints: specify language, framework, error handling approach
  • Give context: tell it the Python version and dependency libraries your project uses

2.3 Context Comprehension Capability ​

Claude Code automatically scans the project directory to understand the codebase:

text
> What API endpoints does this project have?

It will:

  1. Scan the directory structure
  2. Find route definition files
  3. Parse the code to extract endpoint lists
  4. Return results in table format
text
Found the following API endpoints:
| Method | Path | Description |
|---|---|---|
| GET | /api/users | Get user list |
| POST | /api/users | Create new user |
| GET | /api/users/{id} | Get single user |
...

2.4 Output Format ​

Claude Code replies use Markdown format:

  • Code blocks: fenced code blocks with language tags, ready to copy directly
  • Tables: structured data display
  • Lists: steps and key points
  • Bold: key concept highlighting

2.5 Follow-up and Iteration ​

You can continuously follow up and modify generated code:

text
> Add type annotations
> Write unit tests for this function
> Change the exception handling to throw a custom exception instead of ValueError

Each conversation retains the previous context — Claude Code knows which function you're referring to.

Chapter Summary ​

  • Use claude to enter the REPL; /clear clears history, /compact compresses context;
  • Questions should be specific, providing context like language/framework/constraints;
  • Claude Code automatically scans the project directory to understand the codebase;
  • Supports multi-turn follow-ups and iterative modifications.

🧪 随堂测验

点击你认为正确的选项。答错时会展示正确答案与原因解析。

1. Which command clears the current conversation history?

2. When the conversation is long and context is near the limit, what command should you use?

3. Which questioning approach is most likely to get high-quality code?

4. How do you check how many tokens the current session has consumed?

🛠️ Hands-on Practice ​

  1. Use Claude Code in a project to generate a utility function, then follow up to add tests.
  2. Have it list all environment variable references in your project.
  3. Use /cost to check the current session's token usage.

After completing, proceed to Chapter 3.