0Pricing
Vibe Coding · 课时

保持代码可维护

合理组织结构,方便未来修改

保持代码可维护 是 CoddyKit 上的免费 Vibe Coding 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Vibe Coding 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Vibe Coding 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Code Is Read More Than It's Written

AI can generate a working app in minutes. But six weeks later, when you ask it to add a feature, messy code makes everything slower — for both you and the AI. Maintainable code is code that future-you (and future-AI) can change without fear.

This lesson is about keeping a vibe-coded project clean enough to grow.

Clear Names Beat Clever Code

The single highest-impact habit: good names. A function called calculateMonthlyTotal tells you what it does; doStuff2 tells you nothing.

AI sometimes picks lazy names. Just ask it to rename for clarity.

// Hard to follow
function d(x, y) { return x - (x * y / 100); }

// Self-explaining
function applyDiscount(price, percentOff) {
  return price - (price * percentOff / 100);
}

console.log(applyDiscount(100, 20));

Small Functions, One Job Each

A function that does five things is hard to understand and risky to change. Break work into small functions that each do one thing. When you change one, you won't accidentally break the others.

AI is happy to refactor when asked.

Refactor this function so each step is its own small, well-named function. Don't change behavior. Here it is:

function checkout(cart) {
  // calculates totals, applies discount, charges card, sends email...
  <paste the big function>
}

Return the refactored version with a short note on what each function does.

Organize Files by Purpose

A single 3000-line file is a nightmare. Group related code into folders: things about users in one place, payments in another, UI in another. You'll find code faster, and so will the AI.

Ask AI to propose a structure before you reorganize.

My project is one big app.js file. Suggest a clean folder structure for a small web app with users, products, and checkout. Show me which functions go in which file, and explain the reasoning. Don't move anything yet — just propose the layout.

Don't Repeat Yourself

When the same logic appears in three places, a bug fix means three edits — and you'll forget one. Pull repeated logic into a single shared function. This is the DRY principle: Don't Repeat Yourself.

AI loves to copy-paste, so this one needs your eye.

// Repeated formatting in many places
const a = '$' + price1.toFixed(2);
const b = '$' + price2.toFixed(2);

// One source of truth
function formatPrice(value) {
  return '$' + value.toFixed(2);
}
console.log(formatPrice(9.5), formatPrice(120));

Comment the Why, Not the What

Good code explains what it does through clear names. Comments should explain why — the reasoning that the code can't show. AI often over-comments the obvious; trim those and keep the meaningful ones.

// Noise: the code already says this
let count = 0; // set count to zero

// Useful: explains a non-obvious decision
// Stripe rounds to cents, so we store integers to avoid float errors
const amountInCents = Math.round(dollars * 100);

A README So You Don't Forget

Three months from now you won't remember how to run your own app. A README file captures the essentials: what the project is, how to install it, how to run it, and what the environment variables mean.

Have AI generate one from your code.

Read my project and write a README.md with: a one-line description, setup steps (install + run commands), required environment variables and what each is for, and how to run the tests. Keep it short and skimmable.

Keep a Consistent Style

Mixed quotes, random indentation, and inconsistent spacing make code harder to read. A formatter like Prettier auto-fixes all of that, and a linter like ESLint flags risky patterns.

You don't configure these by hand — ask AI to set them up.

Set up Prettier and ESLint for this JavaScript project. Add a "format" and a "lint" script to package.json, sensible default configs, and tell me the one command to auto-format the whole codebase.

Refactor in Small, Safe Steps

The biggest maintainability trap is rewriting everything at once and breaking the app. Instead, clean up in small steps — and lean on the tests you wrote earlier to confirm nothing broke after each change.

Tell the AI to go incrementally.

Refactor this module for readability, but do it in small steps. After each change, keep the behavior identical so my existing tests still pass. List the steps you'd take in order before changing anything.

Avoid Magic Numbers

A raw number like 0.07 buried in code is a mystery. Give it a name. Named constants make the code readable and mean you change the value in one place.

// Magic number — what is 0.07?
function withTax(price) { return price * 1.07; }

// Clear and easy to update
const TAX_RATE = 0.07;
function withTax(price) {
  return price * (1 + TAX_RATE);
}
console.log(withTax(100));

Maintainable Code Helps the AI Too

Here's the payoff specific to vibe coding: clean, well-named, well-organized code makes the AI's future answers better. When the AI can understand your structure, its edits are more accurate and it breaks fewer things.

Maintainability isn't just for humans — it's how you keep your AI partner effective as the project grows.

Quick Check

You notice the same price-formatting logic copy-pasted in seven different files. Following good maintainability habits, what should you do?

Recap: Build to Last

Maintainable code is what lets a vibe-coded project keep growing. Your habits: clear names, small single-purpose functions, organized files, DRY shared logic, meaningful comments, a README, consistent formatting via Prettier/ESLint, and small safe refactors backed by tests.

And remember: clean code makes the AI better at helping you next time. Next up: performance and cost — keeping your app fast and your bills sane.

常见问题解答

「保持代码可维护」课时是免费的吗?

是的 — 「保持代码可维护」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Vibe Coding 课程的其余内容,请升级到 CoddyKit PRO。 Vibe Coding 课程共包含 4 节课。

「保持代码可维护」这节课中我会学到什么?

合理组织结构,方便未来修改 你通过在浏览器中直接运行的动手代码来练习 Vibe Coding,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Vibe Coding 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Vibe Coding 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。

「保持代码可维护」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Vibe Coding 课中编写并运行代码吗?

能。每节 Vibe Coding 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 使用人工智能添加测试
  2. 人工智能代码安全基础
  3. 保持代码可维护
  4. 性能与成本
← 返回 Vibe Coding