当前位置: 首页 > news >正文

代码优化之简化if臃肿的判断条件

简化if判断条件

方法1:

#include <iostream>
#include <vector>
#include <functional>// 封装参数的结构体
struct ConditionParams {int facenum;double zoomRatio;int iso;double facelv;int face_w;double qualityScore;int xx;int yy;
};// 条件检查函数,使用 std::function
bool checkConditions(const ConditionParams& params) {std::vector<std::pair<std::function<bool(const ConditionParams&)>, std::string>> conditions = {{[](const ConditionParams& p) { return p.facenum < p.xx; }, "Face number condition failed."},{[](const ConditionParams& p) { return p.zoomRatio > p.xx; }, "Zoom ratio condition failed."},{[](const ConditionParams& p) { return p.iso < p.xx && p.iso > p.yy; }, "ISO condition failed."},{[](const ConditionParams& p) { return p.facelv > p.xx; }, "Face level condition failed."},{[](const ConditionParams& p) { return p.face_w > p.xx; }, "Face width condition failed."},{[](const ConditionParams& p) { return p.qualityScore > p.xx; }, "Quality score condition failed."}};// 逐个检查条件for (const auto& [checker, errorMessage] : conditions) {if (!checker(params)) {std::cerr << errorMessage << std::endl;return false;}}return true;
}int main() {// 输入的参数ConditionParams params = {5, 2.0, 400, 1.5, 60, 0.95, 10, 100};// 检查条件if (checkConditions(params)) {std::cout << "All conditions met." << std::endl;} else {std::cout << "Some conditions failed." << std::endl;}return 0;
}
  • std::function:使用 std::function<bool(const ConditionParams&)> 取代了函数指针,使得 lambda 表达式更加灵活。
  • ConditionParams 结构体:将所有条件判断的输入参数封装在 ConditionParams 结构体中,使得代码简洁且易于扩展。
  • 简洁的条件检查流程:通过 for 循环遍历所有条件并逐个检查,每个条件不满足时打印对应的错误信息,便于调试。

方法2:

  • 将每个条件提取到独立的函数中:这样每个条件的逻辑更加清晰。
  • 提供清晰的日志或调试信息:当某个条件未通过时,可以打印出相关的错误或状态信息。
  • 使用可读性更好的结构:比如,链式调用或结构体方式,使条件检查更具语义化。
#include <iostream>bool checkFaceNum(int facenum, int threshold) {if (facenum < threshold) {std::cout << "Check failed: facenum < " << threshold << std::endl;return false;}return true;
}bool checkZoomRatio(float zoomRatio, float threshold) {if (zoomRatio <= threshold) {std::cout << "Check failed: zoomRatio <= " << threshold << std::endl;return false;}return true;
}bool checkIsoRange(int iso, int minThreshold, int maxThreshold) {if (iso < minThreshold || iso > maxThreshold) {std::cout << "Check failed: iso out of range [" << minThreshold << ", " << maxThreshold << "]" << std::endl;return false;}return true;
}bool checkFaceLevel(float facelv, float threshold) {if (facelv <= threshold) {std::cout << "Check failed: facelv <= " << threshold << std::endl;return false;}return true;
}bool checkFaceWidth(float face_w, float threshold) {if (face_w <= threshold) {std::cout << "Check failed: face_w <= " << threshold << std::endl;return false;}return true;
}bool checkQualityScore(float qualityScore, float threshold) {if (qualityScore <= threshold) {std::cout << "Check failed: qualityScore <= " << threshold << std::endl;return false;}return true;
}bool allConditionsMet(int facenum, float zoomRatio, int iso, float facelv, float face_w, float qualityScore) {return checkFaceNum(facenum, 10) &&    // 假设阈值为 10checkZoomRatio(zoomRatio, 1.5) &&   // 假设阈值为 1.5checkIsoRange(iso, 100, 800) &&    // 假设iso范围为100-800checkFaceLevel(facelv, 0.8) &&    // 假设facelv阈值为 0.8checkFaceWidth(face_w, 50) &&     // 假设face_w阈值为 50checkQualityScore(qualityScore, 0.9);  // 假设质量分数阈值为 0.9
}int main() {int facenum = 9;float zoomRatio = 2.0;int iso = 400;float facelv = 1.0;float face_w = 55.0;float qualityScore = 0.95;if (allConditionsMet(facenum, zoomRatio, iso, facelv, face_w, qualityScore)) {std::cout << "All conditions met, proceeding..." << std::endl;} else {std::cout << "Conditions not met, please check the logs for details." << std::endl;}return 0;
}

方法3:

如果条件很多,可以使用结构体封装输入参数,并通过链式方法实现条件检查。

#include <iostream>struct ConditionChecker {int facenum;float zoomRatio;int iso;float facelv;float face_w;float qualityScore;bool checkFaceNum(int threshold) {if (facenum < threshold) {std::cout << "Check failed: facenum < " << threshold << std::endl;return false;}return true;}bool checkZoomRatio(float threshold) {if (zoomRatio <= threshold) {std::cout << "Check failed: zoomRatio <= " << threshold << std::endl;return false;}return true;}bool checkIsoRange(int minThreshold, int maxThreshold) {if (iso < minThreshold || iso > maxThreshold) {std::cout << "Check failed: iso out of range [" << minThreshold << ", " << maxThreshold << "]" << std::endl;return false;}return true;}bool checkFaceLevel(float threshold) {if (facelv <= threshold) {std::cout << "Check failed: facelv <= " << threshold << std::endl;return false;}return true;}bool checkFaceWidth(float threshold) {if (face_w <= threshold) {std::cout << "Check failed: face_w <= " << threshold << std::endl;return false;}return true;}bool checkQualityScore(float threshold) {if (qualityScore <= threshold) {std::cout << "Check failed: qualityScore <= " << threshold << std::endl;return false;}return true;}// 链式条件检查bool allConditionsMet() {return checkFaceNum(10) &&checkZoomRatio(1.5) &&checkIsoRange(100, 800) &&checkFaceLevel(0.8) &&checkFaceWidth(50) &&checkQualityScore(0.9);}
};int main() {ConditionChecker checker = {9, 2.0, 400, 1.0, 55.0, 0.95};if (checker.allConditionsMet()) {std::cout << "All conditions met, proceeding..." << std::endl;} else {std::cout << "Conditions not met, please check the logs for details." << std::endl;}return 0;
}

http://www.lryc.cn/news/468163.html

相关文章:

  • 【OpenAI】第六节(语音生成与语音识别技术)从 ChatGPT 到 Whisper 的全方位指南
  • Docker 下备份恢复oracle
  • oneplus3t-android_framework
  • 偷懒总结篇|贪心算法|动态规划|单调栈|图论
  • C语言初阶七:C语言操作符详解(1)
  • GO excelize 读取excel进行时间类型转换(自动转换)
  • 【算法与数据结构】二分查找思想
  • PHP PDO:安全、灵活的数据持久层解决方案
  • 九、Linux实战案例:项目部署全流程深度解析
  • GIS常见前端开发框架
  • Java | Leetcode Java题解之第506题相对名次
  • 数据结构 - 堆
  • html----图片按钮,商品展示
  • YOLOv11改进策略【卷积层】| ECCV-2024 小波卷积WTConv 增大感受野,降低参数量计算量,独家创新助力涨点
  • redis高级篇之redis源码分析List类型quicklist底层演变 答疑159节
  • Elasticsearch 与 Lucene 的区别和联系
  • OpenCV视觉分析之运动分析(5)背景减除类BackgroundSubtractorMOG2的使用
  • 【SAP Hana】X-DOC:数据仓库ETL如何抽取SAP中的CDS视图数据
  • WPF的UpdateSourceTrigger属性
  • 2024-09-25 环境变量,进程地址空间
  • 中国移动机器人将投入养老场景;华为与APUS共筑AI医疗多场景应用
  • 青少年编程能力等级测评CPA C++ 四级试卷(1)
  • 树上任意两点的距离
  • 【 thinkphp8 】00008 thinkphp8数据查询,常用table,name方法,进行数据查询汇总
  • Git的命令合集
  • 博客搭建之路:hexo搜索引擎收录
  • 创建Windows系统还原点
  • Linux等保测评需要用到的命令
  • PostgreSQL的学习心得和知识总结(一百五十六)|auto_explain — log execution plans of slow queries
  • 数据结构模板代码合集(不完整)