Skip to content
Back to skills

使用Qmap重构C 代码消除If Else结构

ASecurity

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

  • 611 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added November 11, 2026
educationc++

Security analysis

A100/100

Scanned November 11, 2026

npx -y skills add ECNU-ICALK/AutoSkill --skill '使用qmap重构c-代码消除if-else结构' --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of 使用Qmap重构C 代码消除If Else结构?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for 使用Qmap重构C 代码消除If Else结构
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/ecnu-icalk-qmap-c-if-else/badge)](https://www.skillsdirectory.com/skills/ecnu-icalk-qmap-c-if-else)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
id: "084d8eff-b77a-4478-9bbe-04dd5a45c45f"
name: "使用QMap重构C++代码消除if-else结构"
description: "针对Qt/C++代码,使用QMap将属性名映射到处理函数,以消除if-else判断结构,实现策略模式重构。"
version: "0.1.0"
tags:
  - "C++"
  - "Qt"
  - "代码重构"
  - "QMap"
  - "策略模式"
triggers:
  - "使用qmap重构代码"
  - "消除if else结构"
  - "封装成一个函数"
  - "refactor code use qmap"
  - "不要使用if else"
---

# 使用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

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in education →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.