Skip to content

4.3 How to Read AI-Generated Code 🟢

After reading this section, you will gain:

  • An understanding of the four core concepts: variables, functions, conditions, and loops
  • The ability to read AI-generated code and understand its logic
  • The skill to describe requirements to AI using pseudocode
  • An understanding that the same functionality can be implemented in multiple ways

All programming languages, no matter how different their syntax may be, are built on a few core concepts.


Introduction

Technical documentation describes what a system is supposed to do, but the final implementation relies on code. You don't need to write every line of code yourself, but you do need to understand the basic logic of code—so you can read AI-generated code, know what it's doing, and troubleshoot when something goes wrong.

There are hundreds of programming languages, each with different syntax, but they all share a few common core concepts. Understanding these concepts is like learning the "alphabet" of reading code.


The Four Basic Building Blocks of Code

变量就像贴了标签的盒子,里面装着数据
点击"赋值"创建变量
let username = "张三"

When you ask AI to generate code, what it's essentially doing is combining four basic elements. Understanding these concepts will help you read code and know what it's doing.

Variables: Containers for Data

A variable is a container for storing data. You can think of it as a labeled box—the data goes inside the box, and the label is the variable name.

For example, let username = "John" creates a box called username with "John" inside it. You can retrieve this value anytime later, or replace the contents of the box with something else.

Variables allow code to "remember" information. A user's login status, the items in a shopping cart, an article title—these are all pieces of data stored in variables.

Functions: Reusable Blocks of Instructions

A function is a reusable block of instructions. When you find yourself writing similar code repeatedly, you should package it into a function.

A function takes input (parameters), performs an operation, and then returns output. For example, a function that calculates the total price of a product:

  • Input: unit price, quantity
  • Process: unit price × quantity
  • Output: total price

Once a function is defined, whenever you need to calculate a total price, you can simply call that function with different parameters instead of rewriting the calculation logic.

Conditions: Forks in the Road

A condition allows a program to take different actions depending on the situation.

if (userIsLoggedIn) {
  showWelcomeMessage
} else {
  showLoginButton
}

This is a conditional statement—the program decides which block of code to execute based on the condition "whether the user is logged in." It's like a fork in the road, where the program chooses which path to take based on a condition.

Loops: The Power of Repetition

A loop allows a program to repeatedly perform certain operations.

For example, if you need to send emails to 1000 users, you don't need to write the send-email code 1000 times. You just write one loop: "for each user in the list, send an email."

The essence of a loop is: use a short description to accomplish a large amount of repetitive work.


Turing Completeness: The Power of These Four Building Blocks

These four concepts—variables, functions, conditions, and loops—form the foundation of Turing completeness. This means that any computable problem can be solved using combinations of these four building blocks. Every app on your phone—calculator, WeChat, Taobao, Douyin—is built on different combinations of these four elements under the hood.

From simple calculators to complex artificial intelligence, from web forms to operating systems, everything underneath is built from different combinations of these four concepts.

When reading code, try to identify these four elements:

  • Where is data stored? → Variables
  • Where are operations encapsulated? → Functions
  • What gets executed under what conditions? → Conditions
  • What is being repeated? → Loops

Pseudocode: A Bridge for Talking with AI

Once you understand the basic building blocks of code, you can read simple code logic. But more importantly, you can use these concepts to describe the functionality you want to AI—and that's what pseudocode is for.

Pseudocode is a way of expressing ideas that sits between natural language and formal code. It uses programming logic structures (such as conditions and loops) to describe requirements, but it doesn't need to follow any specific syntax.

For example, if you want AI to help you write a user login feature, you can describe it with pseudocode:

When the user clicks the login button:
    Get the email and password from the input fields
    Check if the email format is valid
    If the format is correct:
        Send a request to the server for verification
        If verification succeeds:
            Redirect to the homepage
        Otherwise:
            Show "Incorrect password"
    Otherwise:
        Show "Invalid email format"

This way of describing requirements is clearer than plain natural language, while still not requiring you to master specific syntax. AI can understand pseudocode very well and help convert it into formal code.

💬自然语言

检查购物车里每件商品,如果库存不足就标红,最后算出总价

📝伪代码
对于 购物车中的每件商品:
  如果 商品库存 < 购买数量:
    标记为"库存不足"
  总价 += 商品单价 × 购买数量
返回 总价
🔷TypeScript
function calculateTotal(cart: CartItem[]) {
  let total = 0
  for (const item of cart) {
    if (item.stock < item.quantity) {
      item.status = 'insufficient'
    }
    total += item.price * item.quantity
  }
  return total
}

Algorithmic Thinking: Why the Same Functionality Can Have Different Code

When you describe requirements with pseudocode, AI may provide different implementation approaches. These approaches can all achieve the same functionality, but the steps may differ.

For example: finding a specific email address among 1000 users.

Approach 1: Check them one by one. In the worst case, you need to check all 1000.

Approach 2: If the users are sorted by email, start by checking the one in the middle. If the target email comes later, then you only need to check the second half, and repeat the process. This way, you need at most 10 checks.

Both approaches can complete the task, but the second one takes fewer steps. When asking AI to generate code, if large amounts of data are involved, you can simply remind it that "the data volume is large, please use an efficient search method"—AI will understand what you mean.

目标值: 23
线性搜索
1
3
5
7
9
11
13
15
17
19
21
23
25
27
29
31
等待开始
二分搜索
1
3
5
7
9
11
13
15
17
19
21
23
25
27
29
31
等待开始

Key Takeaways

  • ✅ Variables are containers for storing data
  • ✅ Functions are reusable units that encapsulate operations
  • ✅ Conditions allow a program to branch its execution
  • ✅ Loops allow a program to repeat execution
  • ✅ Pseudocode is a tool for describing requirements using programming logic
  • ✅ The same functionality can be implemented in different ways, and AI will choose an appropriate approach

Now that you've understood the basic building blocks of programming, next you'll learn the fundamentals of API and HTTP communication.


Alpha Preview:This is an early internal build. Some chapters are still incomplete and issues may exist. Feedback is very welcome on GitHub.