control-flow
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseHuman-Readable Control Flow
易读性控制流
When refactoring complex control flow, mirror natural human reasoning patterns:
- Ask the human question first: "Can I use what I already have?" -> early return for happy path
- Assess the situation: "What's my current state and what do I need to do?" -> clear, mutually exclusive conditions
- Take action: "Get what I need" -> consolidated logic at the end
- Use natural language variables: ,
isUsingNavigator,isUsingLocalTranscription: names that read like thoughtsneedsOldFileCleanup - Avoid artificial constructs: No nested conditions that don't match how humans actually think through problems
Transform this: nested conditionals with duplicated logic
Into this: linear flow that mirrors human decision-making
在重构复杂控制流时,模仿人类自然的推理模式:
- 先提出人类视角的问题:"我能否使用已有的内容?" -> 针对理想路径提前返回
- 评估当前状况:"我当前的状态是什么?我需要做什么?" -> 清晰、互斥的条件判断
- 采取行动:"获取我需要的内容" -> 将逻辑整合在最后
- 使用自然语言风格的变量名:,
isUsingNavigator,isUsingLocalTranscription:名称读起来就像自然的想法needsOldFileCleanup - 避免人为构造的复杂结构:不要使用不符合人类实际思考问题方式的嵌套条件
将这样的代码:带有重复逻辑的嵌套条件语句
转化为这样的代码:符合人类决策逻辑的线性流程
Example: Early Returns with Natural Language Variables
示例:使用自然语言变量的提前返回
typescript
// From apps/whispering/src/routes/(app)/_layout-utils/check-ffmpeg.ts
export async function checkFfmpegRecordingMethodCompatibility() {
if (!window.__TAURI_INTERNALS__) return;
// Only check if FFmpeg recording method is selected
if (settings.value['recording.method'] !== 'ffmpeg') return;
const { data: ffmpegInstalled } = await rpc.ffmpeg.checkFfmpegInstalled.ensure();
if (ffmpegInstalled) return; // FFmpeg is installed, all good
// FFmpeg recording method selected but not installed
toast.warning('FFmpeg Required for FFmpeg Recording Method', {
// ... toast content
});
}typescript
// From apps/whispering/src/routes/(app)/_layout-utils/check-ffmpeg.ts
export async function checkFfmpegRecordingMethodCompatibility() {
if (!window.__TAURI_INTERNALS__) return;
// Only check if FFmpeg recording method is selected
if (settings.value['recording.method'] !== 'ffmpeg') return;
const { data: ffmpegInstalled } = await rpc.ffmpeg.checkFfmpegInstalled.ensure();
if (ffmpegInstalled) return; // FFmpeg is installed, all good
// FFmpeg recording method selected but not installed
toast.warning('FFmpeg Required for FFmpeg Recording Method', {
// ... toast content
});
}Example: Natural Language Booleans
示例:自然语言风格的布尔值
typescript
// From apps/whispering/src/routes/(app)/_layout-utils/check-ffmpeg.ts
const isUsingNavigator = settings.value['recording.method'] === 'navigator';
const isUsingLocalTranscription =
settings.value['transcription.selectedTranscriptionService'] === 'whispercpp' ||
settings.value['transcription.selectedTranscriptionService'] === 'parakeet';
return isUsingNavigator && isUsingLocalTranscription && !isFFmpegInstalled;typescript
// From apps/whispering/src/routes/(app)/_layout-utils/check-ffmpeg.ts
const isUsingNavigator = settings.value['recording.method'] === 'navigator';
const isUsingLocalTranscription =
settings.value['transcription.selectedTranscriptionService'] === 'whispercpp' ||
settings.value['transcription.selectedTranscriptionService'] === 'parakeet';
return isUsingNavigator && isUsingLocalTranscription && !isFFmpegInstalled;Example: Cleanup Check with Comment
示例:带注释的清理检查
typescript
// From packages/epicenter/src/indexes/markdown/markdown-index.ts
/**
* This is checking if there's an old filename AND if it's different
* from the new one. It's essentially checking: "has the filename
* changed?" and "do we need to clean up the old file?"
*/
const needsOldFileCleanup = oldFilename && oldFilename !== filename;
if (needsOldFileCleanup) {
const oldFilePath = path.join(tableConfig.directory, oldFilename);
await deleteMarkdownFile({ filePath: oldFilePath });
tracking[table.name]!.deleteByFilename({ filename: oldFilename });
}typescript
// From packages/epicenter/src/indexes/markdown/markdown-index.ts
/**
* This is checking if there's an old filename AND if it's different
* from the new one. It's essentially checking: "has the filename
* changed?" and "do we need to clean up the old file?"
*/
const needsOldFileCleanup = oldFilename && oldFilename !== filename;
if (needsOldFileCleanup) {
const oldFilePath = path.join(tableConfig.directory, oldFilename);
await deleteMarkdownFile({ filePath: oldFilePath });
tracking[table.name]!.deleteByFilename({ filename: oldFilename });
}