好的变量名就是等待被说出口的英语。在这节课里,你将学会 TypeScript 开发者如何命名,以及他们选用的词汇如何体现其思维方式。
Good variable names are just good English sentences waiting to happen. In this lesson, you'll learn how TypeScript developers name things — and why the words they choose reveal their thinking.
代码精读
// 🚫 Bad naming — a computer could read this, but a human cannot
const x = 86400;
const arr = ['Alice', 'Bob', 'Charlie'];
// ✅ Good naming — English tells the story
const SECONDS_PER_DAY = 86400;
const activeUsers: string[] = ['Alice', 'Bob', 'Charlie'];
// 📖 Notice how the variable name answers a question:
// 'What do these users have in common?' → they are 'active'
// Using descriptive verbs for boolean variables:
const isLoggedIn: boolean = true; // is... → state question
const hasPermission: boolean = false; // has... → possession question
const shouldRedirect: boolean = true; // should... → intent question
💡 建议:把每一行代码大声用英文念出来,注释就是你的翻译。
单词讲解
descriptive
/dɪˈskrɪptɪv/
adjective
描述性的,能清晰说明特征的
convention
/kənˈvɛnʃn/
noun
惯例,约定俗成的规范
intent
/ɪnˈtɛnt/
noun
意图,目的
boolean
/ˈbuːliən/
noun / adjective
布尔值(true/false)
理解检验
先在脑子里想一想——再点开看参考答案。
Q1: Why is `SECONDS_PER_DAY` better than `x`?
Q2: What prefix pattern is used for boolean variables in the example?
核心要点▼ 展开