Understanding Existing Code

Inherited an undocumented project? Onboarding to a new codebase? AI can read it, explain it, and document it faster than you can read it yourself.

Beginner 10 min read Lesson 9 of 20

Start With a Map

Before diving into individual files, get the AI to give you a mental map of the project. In Claude Code, navigate to the project root and ask:

Project orientation prompts
# Get a high-level overview:
> Read the directory structure and main files.
  Describe this project in 3-5 sentences: what does it do,
  what technology stack does it use, and how is it organized?

# Understand the entry points:
> Where does a web request enter this application?
  Trace the path from the URL to the first PHP/Python file that handles it.

# Find the database:
> What database tables exist in this project?
  Find schema files, migration files, or CREATE TABLE statements.
  List each table and its main purpose.

# Map the major components:
> What are the main "areas" of this codebase?
  For example: auth, products, orders, admin panel.
  Which files/directories handle each area?

Explain Code Prompts

Once you have the map, zoom in on specific pieces:

Line-level explanation
> Explain this code line by line in simple terms:
  [paste 10-30 lines of code]

> What does this function do and what does it return?
  [paste function]

> I see this pattern used everywhere in this codebase:
  [paste code pattern]
  What is this pattern called and why is it used here?

> This code uses the Repository pattern. Can you explain it in
  simple terms and show me which classes are repositories in this project?
Class-level explanation
> Read the UserModel class. Describe:
  1. What data it manages
  2. What each public method does (one sentence each)
  3. Any non-obvious design choices
  4. What other classes depend on it

> Read the AuthController. Walk me through the login flow step by step,
  from when the form is submitted to when the user reaches the dashboard.

Trace Data Flow

One of the most valuable uses of AI: tracing how data moves through a system.

Data flow tracing prompts
# Trace a user action:
> Trace exactly what happens when a user clicks "Place Order" on the checkout page.
  Follow the code path: which files are executed in which order,
  what data is passed between them, and what is ultimately saved to the database.

# Trace data through layers:
> How does a product get from the database to the HTML page?
  Trace from the SQL query through any transformations to the final output.

# Find where something is set/read:
> In which files is the $_SESSION['user_id'] variable set and read?
  Find every place it is used and what it is used for.

# Find dependencies:
> Which parts of the code depend on the PaymentGateway class?
  If I change its interface, which files would break?

Document With AI

Turn undocumented code into well-documented code using AI:

Documentation generation prompts
# Add PHPDoc to all methods:
> Add PHPDoc comments to every public method in this class.
  Include @param, @return, and a one-sentence description.
  [paste class]

# Write a README section:
> Read the /app/services/ directory. Write a README.md section
  that explains what each service class does and when to use it.

# Document the database schema:
> Read all the CREATE TABLE statements in /database/schema.sql.
  Write a DATABASE.md file that describes each table's purpose,
  important columns, and relationships to other tables.

# Document a complex algorithm:
> This function implements a complex calculation. Add inline comments
  that explain the purpose of each step, not just what the code does.
  [paste function]

Onboarding Prompts

Use these when joining a new project or picking up an old one after a long break:

Complete onboarding session
# Step 1: Get oriented (run this first in a new project):
> I am new to this codebase. Read the project files and answer:
  1. What does this application do?
  2. What is the tech stack?
  3. How do I run it locally?
  4. Where is the main configuration?
  5. What are the 5 most important files to understand first?

# Step 2: Understand the architecture:
> Describe the architecture of this application.
  How is the code organized? Does it use MVC, service layer,
  repository pattern, or something else?

# Step 3: Find the important stuff:
> Where is:
  - User authentication handled?
  - The database connection configured?
  - The main routing logic?
  - Email sending?
  - Error logging?

# Step 4: Understand the data model:
> Read the database schema and draw (in text) the entity relationships.
  Which tables are central and which are peripheral?

Before You Change Anything

Before modifying any code in an unfamiliar codebase, use AI to understand the risk:

Pre-change analysis prompts
> I want to change the getUserById() method in UserModel.
  Which other files call this method?
  What would break if I change its return type from array to object?

> I want to rename the 'email' column in the users table to 'email_address'.
  Find every place in the codebase that references 'email' in a database query
  or result. List the files and lines I would need to update.

> I want to add a required 'tenant_id' parameter to the UserModel constructor.
  How many places create a new UserModel? List them all.

> Before I modify the checkout flow, which automated tests cover it?
  Run the test suite and show me which tests would be affected.
Build a knowledge document as you learn

As you use AI to understand a project, keep a running NOTES.md file. After each AI explanation, write a 2-3 sentence summary in your own words. This forces comprehension and gives you a personal reference document. After a day of AI-assisted onboarding, you will have documentation that would normally take a week to write manually.

Frequently Asked Questions

Legally: check your company's IP policies. If the code is under a proprietary license or confidential, check whether sending it to a third-party AI service is permitted. For open-source code, public code, or your own code, there are no legal concerns.

Ask follow-up questions. "I don't understand the part about [specific thing]. Can you explain it differently?" or "Give me an analogy for how this pattern works." Keep asking until you genuinely understand. There is no shame in needing multiple rounds of explanation.

Yes, with verification. Ask the AI to document what it understands from reading the code, then verify the documentation by testing the actual behavior. AI-generated docs are a great starting point that you then own and maintain. They are always better than no documentation.