Skip to content
AI Growth Lab
Navigation menu
AI Growth Lab

Loading lesson…

CoursesContactPrivacy

Curriculum based on Datawhale / Easy Vibe · CC BY-NC-SA 4.0

← Back to course

AI-Native Engineer

Claude Code Fundamentals
  1. Getting Started with Claude Code
  2. Essential Claude Code Techniques
  3. Configuring Claude Code for Your Project
  4. Everyday Development Workflows
  5. Troubleshooting Claude Code
MCP: Connecting Claude Code to Tools
  1. Getting Started with MCP in Claude Code
  2. Using and Troubleshooting MCP
  3. Configuring Common MCP Servers
Claude Code Skills
  1. Finding and Using Your First Skills
  2. Understanding Skills and SKILL.md
  3. Creating and Improving Your Own Skills
  4. How Claude Skills Work Internally
Claude Agent Teams
  1. Understanding and Setting Up Agent Teams
  2. Build an RPG with Agent Teams
  3. Agent Teams Best Practices
  4. Choosing When to Use Agent Teams
AI-Assisted Development Workflows
  1. Choosing an AI Development Strategy
  2. Developing Features and Fixing Bugs with AI
  3. Refactoring and Reviewing Code with AI
  4. Building a Project Knowledge Base
  5. Improving AI Collaboration Efficiency
Building Agents with Claude Agent SDK
  1. Getting Started with Claude Agent SDK
  2. Advanced Features and Practical Agent Examples
  3. Building a Code-Quality Guardrail Pipeline
Engineering Workflows with Superpowers
  1. Getting Started with Superpowers and Its Core Skills
  2. Build an Authentication System with Superpowers
  3. Configuring and Using Superpowers Effectively
Spec Coding
  1. Spec Coding Foundations
  2. Practicing Spec Coding in Claude Code
  3. Moving from Vibe Coding to Spec Coding
  4. Spec Coding FAQ and Summary
GitHub Spec-Driven Development
  1. Workflow Foundations and Setup
  2. From Product Idea to Specification
  3. From Specification to Working Software
  4. Reusable Prompts and Responsible Autonomy
Cross-Platform Development
  1. From a Web Page to Real Software and an AI-Native Product
  2. How to Choose the Right Platform for Your App
  3. How to Build an Android App with Jetpack Compose
  4. How to Build an iOS App with SwiftUI
  5. Build a Store Inspection App with React Native and Expo
On this page
  • 3.3 Refactoring Code
  • 3.4 Code Review

3.3 Refactoring Code

Scenario: the code has become hard to maintain, and it needs refactoring to improve code quality.

Complete workflow:

Step 1: Define the refactoring goal (done by you)

Clarify the following:

  • What problem are you trying to solve? (duplicate code? messy structure? performance issues?)

Lesson Contents

  1. 3.3 Refactoring Code
  2. 3.4 Code Review
AI Development FAQ and Summary
How to Build a Cross-Platform App with Flutter
  • How to Build a PWA Local App
  • How to Build a Browser AI Assistant Extension
  • How to Build a Cross-Platform Electron Desktop App
  • How to Build Platform, Pixel-Art, and 3D Games with Godot
  • How to Build a VS Code Extension
  • How to Build an Industrial Qt Desktop App
  • How to Build Your Personal Website and Academic Blog
  • How to Publish the Application You Built
  • How large is the scope of the refactor?
  • What level of risk is acceptable?
  • Step 2: Make a refactoring plan (with AI)

    These three functions (getUserList, getArticleList, getCommentList)
    contain a lot of duplicated code for pagination, sorting, and filtering.
    
    Please help me:
    1. Analyze the duplicated parts
    2. Propose a refactoring plan
    3. Evaluate the risk of the refactor
    

    Step 3: Prepare a safety net (ask AI to help)

    Before refactoring, please write test cases for me first
    so that the behavior stays the same after the refactor
    

    Step 4: Refactor step by step (AI executes, you verify)

    According to the plan we just discussed, first extract the shared function
    
    Now optimize the data structure
    
    Finally update the call sites
    

    Step 5: Verify the result (done by you)

    • Run all tests and make sure they pass
    • Manually test key functionality
    • Check whether code quality has improved

    Real conversation example:

    You: These three functions have a lot of duplicate code. Can you help me refactor them?
        [Paste the code of the three functions]
    
    AI: I analyzed them, and the main differences are:
        - getUserList: gets the user list
        - getArticleList: gets the article list
        - getCommentList: gets the comment list
    
        Their common logic is pagination, sorting, and filtering.
    
        Refactoring plan:
        1. Extract a generic getList function
        2. Pass the different parts as parameters
    
        This can reduce 80% of the duplicated code.
    
        Should I implement it?
    
    You: Yes, but first help me write test cases
    
    AI: [Generates tests]
        I have written tests for all three functions,
        covering the main use cases.
    
        Can I start refactoring now?
    
    You: Yes, start refactoring
    
    AI: [Refactors the code]
        Refactoring is complete. I created a generic getList function,
        and the original three functions now all call that shared function.
    
        Please run the tests to confirm everything works: npm test
    

    3.4 Code Review

    Scenario: a teammate submitted code, and you need to review it.

    Complete workflow:

    Step 1: Quickly understand the change (ask AI to help)

    Summarize what this PR changed
    Which files were mainly modified
    

    Step 2: Check common issues (ask AI to help)

    Check this change for:
    1. Security issues (SQL injection, XSS, etc.)
    2. Performance problems (N+1 queries, memory leaks, etc.)
    3. Obvious bugs
    4. Whether the coding style follows the project conventions
    

    Step 3: Deep review (done by you)

    • Whether the business logic is correct
    • Whether edge cases were considered
    • Whether there is a better implementation approach
    • Whether tests should be added

    Step 4: Give feedback (done by you)

    • Point out the places that need changes
    • Explain why they should be changed
    • Provide improvement suggestions

    Real conversation example:

    You: Help me review this PR
        [Provide the PR link or code]
    
    AI: I reviewed the change and found these issues:
    
        1. Security issues:
           - Line 23: user input is not escaped, which may lead to XSS risk
           - Line 45: the SQL query is built by string concatenation, which risks injection
    
        2. Performance problems:
           - Line 67: the database is queried inside a loop, which creates an N+1 problem
    
        3. Code quality:
           - The handleSubmit function is too long (200 lines); consider splitting it
           - Error handling is missing
    
        Would you like me to explain any of these in more detail?
    
    You: Explain the SQL injection issue in detail
    
    AI: On line 45:
        ```javascript
        const query = `SELECT * FROM users WHERE name = '${userName}'`;
        ```
    
        This directly concatenates user input into the SQL statement.
        If a user enters `' OR '1'='1`, it becomes:
        ```sql
        SELECT * FROM users WHERE name = '' OR '1'='1'
        ```
        This would return all user data.
    
        The correct approach is to use a parameterized query:
        ```javascript
        const query = 'SELECT * FROM users WHERE name = ?';
        db.query(query, [userName]);
        ```
    
        Would you like me to fix it?
    
    Previous
    Sign in to track progress
    Next