Foundations

Understanding Unfamiliar Codebases

Lesson 3 of 4 Estimated Time 45 min

Understanding Unfamiliar Codebases

You’ve all been there: starting a new job, joining a new project, or working on legacy code you’ve never seen. The codebase is large, the patterns are unfamiliar, and the architecture is unclear. This is where AI shines. Instead of spending days reading code, you can ask the AI to guide you through it.

The Challenge of Unfamiliar Code

When you encounter unfamiliar code, you face:

  1. Unknown patterns — The team’s way of doing things
  2. Implicit assumptions — Things not documented
  3. Large scope — Thousands of lines to understand
  4. Historical decisions — “Why did they do it this way?”

AI can help navigate all of these.

Using AI for Codebase Exploration

Technique 1: The Architectural Overview

When you first encounter a project:

You: "I'm new to this project. Here's the directory structure:
[paste directory listing]

What does this project do and how is it organized?"

AI: [Explains purpose, architecture, major components]

The AI gives you a mental model of how the project is organized.

Technique 2: The File-by-File Walkthrough

For key files, ask the AI to explain:

You: "What does this file do?
[paste main.ts or index.js]

How does it fit into the project?"

AI: [Explains purpose and relationships to other files]

You can build understanding file by file.

Technique 3: The Feature Walkthrough

To understand how features work:

You: "How does user authentication work in this codebase?
     Where does it start? What files are involved?"

AI: [Traces feature through the codebase]
"User login starts here [file], calls this [function],
which validates against [auth service], and returns [token]"

This traces the execution path, teaching you the feature.

Technique 4: The Pattern Identification

Ask the AI to identify patterns:

You: "What patterns does this codebase use?
     How do you handle errors?
     How do you manage state?
     How do you structure API endpoints?"

AI: [Lists patterns and gives examples]

Understanding patterns teaches you the team’s thinking.

Generating Documentation for Existing Code

Creating Architecture Documentation

You: "Describe the high-level architecture of this codebase
     as if I had never seen it.
     Directory structure: [paste]
     Key files: [paste main files]"

AI: [Generates architecture overview]

You can then format this as documentation.

Creating API Documentation

You: "These are all our API endpoints:
     [paste endpoint code]

     Generate OpenAPI/Swagger documentation"

AI: [Generates complete API docs]

Creating Data Flow Documentation

You: "How does data flow through this system?
     Here's the entry point: [code]
     Here are the main services: [code]
     Here's the database schema: [schema]

     Document the complete data flow from request to response"

AI: [Explains and diagrams data flow]

Exploring Legacy Code

Legacy code is particularly hard to understand. It may use old patterns, have accumulated complexity, and lack documentation.

Understanding Legacy Patterns

You: "I'm looking at legacy code using this pattern:
     [old pattern code]

     What is this doing? Why might they have built it this way?
     How would we write this today?"

AI: [Explains old pattern and modern equivalent]

This helps you understand both the legacy code and its modern alternative.

Refactoring Legacy Code Safely

You: "I need to refactor this legacy function:
     [paste function]

     First, what does it do?
     Then, how would we write it today?
     Finally, what tests would I need to ensure I don't break it?"

AI: [Explains current behavior, modern approach, tests needed]

This guides you through safe refactoring of unknown code.

Often you don’t have access to the entire codebase (it’s too large, or you only have relevant parts).

When Context Is Limited

You: "I'm looking at this function:
     [paste function you're trying to understand]

     I don't have the full codebase, but I'm trying to understand
     what this does. Can you help?"

AI: "Based on the function signature, imports, and logic,
    this appears to do X. It depends on [external functions]
    which probably handle Y. If you can show me those functions,
    I can explain it better."

The AI fills gaps with reasonable inferences.

Asking Targeted Questions

You: "Looking at this code, I have questions:
     1. What does this return value represent?
     2. When would this condition be true?
     3. What's the purpose of this data structure?
     [paste relevant code]"

AI: [Answers specific questions about the code]

Targeted questions are more effective than “explain everything.”

Learning Through Code Exploration

Code Reading as Learning

You: "Here's a function I don't understand:
     [paste function]

     Walk me through it line by line, explaining what each part does."

AI: [Line-by-line explanation]

This teaches you both the code and the patterns used.

Understanding Design Patterns

You: "This code seems to use a pattern I'm not familiar with:
     [paste code]

     What pattern is this? Why use this pattern?
     What are alternatives?"

AI: [Explains the pattern, its benefits, and alternatives]

You learn both about this specific code and about design patterns.

Exploring Alternative Approaches

You: "This function solves a problem:
     [paste function]

     What are other ways to solve the same problem?
     What are pros/cons of each approach?"

AI: [Lists alternatives and tradeoffs]

This broadens your thinking beyond the current code.

Common Scenarios

Scenario 1: “Why Does This Exist?”

You: "I found this function that seems unnecessary:
     [paste function]

     Does anything call this? Why might it exist?"

AI: "Let me think... it's probably called from:
    [likely callers]
    It might exist because:
    [historical reasons or edge cases]"

Understanding why helps decide whether to remove or keep it.

Scenario 2: “How Do These Pieces Fit?”

You: "I see these three functions:
     function A does X
     function B does Y
     function C does Z

     How do they work together?
     [paste all three]"

AI: [Explains how they interact]

Integration points are clearer when you ask.

Scenario 3: “What Could I Break?”

You: "I'm considering modifying this function:
     [paste function]

     What depends on this function? What could I break?"

AI: "This function is called from [other functions].
    If you change X, it might break [these scenarios].
    Make sure to update [related functions]."

Dependency analysis prevents breaking changes.

Building Mental Models

The Mental Model

Understanding a codebase isn’t about reading every line. It’s about building a mental model:

Mental model of project:
1. Entry point: request comes here
2. Routing: request goes to controller
3. Controller: validates input, calls service
4. Service: business logic, calls repository
5. Repository: data access, returns data
6. Response: data is serialized and returned

Once you understand this pattern, you understand the entire
architecture.

Building the Model with AI

Step 1: Ask for high-level overview
"What is the overall architecture?"

Step 2: Ask about each component
"What does the service layer do?"

Step 3: Ask about interactions
"How do components talk to each other?"

Step 4: Ask about data flow
"How does data flow from request to response?"

Step 5: Ask about exceptions
"What doesn't fit the pattern? What's different?"

In 5-10 minutes, you have a mental model.

Creating a Knowledge Base

As you learn a codebase, document it:

README for Yourself

Create a file documenting the codebase:

# Project Overview

## What It Does
[Purpose of the project]

## Architecture
[High-level architecture]

## Key Components
- Component A: [purpose]
- Component B: [purpose]

## Data Flow
[How data flows through the system]

## Common Tasks
- To add a feature: [steps]
- To fix a bug: [steps]
- To optimize: [steps]

## Gotchas
- [Unexpected behaviors]
- [Historical decisions]
- [Anti-patterns]

Ask AI to Populate It

You: "Based on this codebase, create a README documenting:
     1. What the project does
     2. High-level architecture
     3. Key components
     4. How to add new features

     [paste relevant code]"

AI: [Generates markdown README]

This documentation helps future you (and teammates).

Real-World Scenario: Jumping into a New Project

Day 1: High-Level Understanding
You: "What is this project?
     [paste directory structure]"
AI: "This is an e-commerce platform built with Node.js,
    MongoDB, and React..."

Day 2: Architecture Understanding
You: "How is it organized?
     [paste main files]"
AI: "Uses MVC pattern with controllers, services, models..."

Day 3: Feature Understanding
You: "How does the checkout flow work?
     [paste checkout files]"
AI: "User selects items, calls checkout endpoint, which..."

Day 4: Code Contribution
You: "I need to add a new payment method. Where do I add code?
     [paste current payment code]"
AI: "Add a new PaymentStrategy here, update this service,
    test with this approach..."

By Day 4, you're productive in code you didn't write.

Exercises

  1. New Project Exploration: Start a new open-source project. Use AI to understand:

    • What it does
    • How it’s organized
    • Key components Document your understanding.
  2. Architecture Documentation: Pick a project you know. Ask AI to generate architecture documentation from the code. Compare to official docs (if they exist).

  3. Feature Trace: Pick a feature. Ask AI to trace how it works:

    • Entry point
    • Each function called
    • Data transformations
    • Exit point Document the complete flow.
  4. Pattern Learning: Find a design pattern in code you don’t fully understand. Ask AI to:

    • Explain the pattern
    • Show alternatives
    • Explain when to use it Learn the pattern deeply.
  5. Legacy Code Exploration: Find old/legacy code. Ask AI:

    • What is it doing?
    • Why might it be written this way?
    • How would we write it today?
    • What tests would make it safe to refactor?