Skip to content
使用QMap重构C++代码消除if-else结构 logo

使用QMap重构C++代码消除if-else结构

针对Qt/C++代码,使用QMap将属性名映射到处理函数,以消除if-else判断结构,实现策略模式重构。

ECNU-ICALK/AutoSkill0installs596starsOther

SKILL.md

Full skill instructions

使用QMap重构C++代码消除if-else结构

针对Qt/​C++代码,使用QMap将属性名映射到处理函数,以消除if-else判断结构,实现策略模式重构。

Prompt

Role & Objective

你是一个C++/​Qt代码重构专家。你的任务是将包含多个if-else判断的代码重构为使用QMap和std::function的策略模式,以消除显式的条件判断结构。

Operational Rules & Constraints

  1. 使用QMap进行映射:创建一个QMap<QString, std::function<void(...)>>,将属性名(key)映射到对应的处理逻辑(lambda或函数)。
  2. 消除if-else:严禁在主逻辑中使用if-else链来根据属性名分发逻辑。必须通过QMap的查找机制来调用对应的处理函数。
  3. 遍历与查找:遍历源对象(如QJsonObject或QVariantMap),检查属性名是否存在于QMap中,若存在则调用对应的处理函数。
  4. 类型处理:在处理函数内部,正确处理QVariant类型。如果需要区分字符串和整数,使用isString()或canConvert()进行检查,而不是在外部if-else中判断。
  5. 函数封装:对于重复的逻辑或特定的处理逻辑,应封装为独立的命名函数,并在QMap中引用该函数。

Anti-Patterns

  • 不要在循环中使用if (name == "A") ... else if (name == "B") ... 的结构。
  • 不要忽略用户对特定类型转换的要求(如toString vs toInt)。

Triggers

  • 使用qmap重构代码
  • 消除if else结构
  • 封装成一个函数
  • refactor code use qmap
  • 不要使用if else

More Other skills

Delegate independent tasks to specialized agents working concurrently with isolated context.

295.4K 206.3K
View

Applies Anthropic's official brand colors and typography to any sort of artifact that may benefit from having Anthropic's look-and-feel. Use it when brand colors or style guidelines, visual formatting, or company design standards apply.

179.7K 100.1K
View

Toolkit for styling artifacts with a theme. These artifacts can be slides, docs, reportings, HTML landing pages, etc. There are 10 pre-set themes with colors/fonts that you can apply to any artifact that has been creating, or can generate a new theme on-the-fly.

179.7K 89.5K
View

A set of resources to help me write all kinds of internal communications, using the formats that my company likes to use. Claude should use this skill whenever asked to write some sort of internal communications (status reports, leadership updates, 3P updates, company newsletters, FAQs, incident reports, project updates, etc.).

179.7K 76.1K
View

Fundamental design principles for writing maintainable, testable Python code.

40.2K 21.9K
View

Modern responsive layouts using container queries, fluid typography, CSS Grid, and mobile-first strategies.

40.2K 20K
View

Generate Playwright tests from scenarios using interactive browser exploration and validation.

39.7K 18.2K
View

Modern React state management with Redux Toolkit, Zustand, Jotai, and React Query for every state category.

40.2K 14.7K
View

GDPR-compliant data handling with consent management, data subject rights, and privacy controls.

40.2K 14.3K
View

Python error handling patterns including input validation, exception hierarchies, and partial failure handling. Use when implementing validation logic, designing exception strategies, handling batch processing failures, or building robust APIs.

40.2K 13.5K
View

Detailed multi-file refactor planning with safety checks and rollback guidance.

39.7K 13K
View