164
社区成员
发帖
与我相关
我的任务
分享| 元素类型 | 命名规则 | 示例 |
|---|---|---|
| 变量 / 常量 | 局部变量:小驼峰(camelCase); 常量:全大写 + 下划线(UPPER_SNAKE_CASE),优先用 constexpr / const | 局部变量:imagePath;常量: MAX_FILTER_SIZE = 3 |
| 函数 / 方法 | 小驼峰 + 动词开头,避免单字母命名 | loadImage()、applyGaussianBlur() |
| 类 / 结构体 | PascalCase,体现对象本质 | ImageProcessor、EdgeDetector |
| 命名空间 | 全小写,可按模块 / 项目名划分 | namespace image_processing |
| 宏定义 | 全大写 + 下划线,仅用于必要的编译期常量 | #define IMAGE_WIDTH 640 |
| 枚举类型 | 枚举名用帕斯卡,枚举值全大写 + 下划线 | enum ImageType { IMAGE_GRAYSCALE, IMAGE_COLOR }; |
| 模板参数 | 单个大写字母或有意义的单词 | template <typename T>、template <typename PixelType> |
{ 与声明同行(如 if (cond) {),} 单独占行并与声明对齐。+、&&)后换行,缩进 2 级(8 个空格),例如:result = cv::addWeighted(originalImage, alpha,
overlayImage, beta, gamma);
R"()" 原始字符串或拼接。+、=、==)前后、逗号后必须加空格(如 a = b + c,非 a=b+c)。if (cond) doA(); else doB();(需分行)。int sum = 0;,非 int sum;),局部变量尽量靠近使用位置。文件头部注释:包含文件名、作者、功能、创建时间、修改记录,例如:
/*
* File: edge_detection.cpp
* Author: XXX
* Function: Perform edge detection on images using OpenCV
* Create Time: 2025-09-10
* Modify Record: None
*/
函数 / 类注释:描述功能、参数(含义 / 取值范围)、返回值、异常,优先用 Doxygen 格式,例如:
/**
* @brief Apply Gaussian blur to an image
* @param inputImage Input image (must not be empty)
* @param kernelSize Size of the Gaussian kernel (must be odd)
* @return Blurred image (cv::Mat)
* @throw std::invalid_argument If input image is empty or kernel size is even
*/
cv::Mat applyGaussianBlur(const cv::Mat& inputImage, int kernelSize);
行内注释:仅解释复杂逻辑(如 // Skip pixels with intensity below threshold),禁止冗余注释(如 i++;// Increment i)。
TODO 注释:标记待完善内容(如 // TODO: Optimize image resizing algorithm),删除无用代码(禁止注释掉旧代码)。
applyHistogramEqualization()、sharpenImage()、adjustBrightness())。struct ImageProcessingParams { int blurKernelSize; double brightnessFactor; };)。private,通过 getter/setter 访问(需验证逻辑时),避免直接暴露。if→for→if 可拆分为子函数),优先 “早返回”(如 if (image.empty()) return;)。goto 语句(仅在异常处理等特殊场景谨慎使用)。image_processor.cpp 不包含视频处理逻辑)。filters.h / filters.cpp,“边缘检测模块” 含 edge_detectors.h / edge_detectors.cpp)。std::unique_ptr / std::shared_ptr)管理动态内存,避免裸指针(如 auto imageData = std::make_unique<uchar[]>(width * height);)。std::string 替代 char[],用 std::vector / std::map 替代原生数组(如 std::vector<cv::KeyPoint> keypoints;)。constexpr(编译期常量)或 const(运行期常量),避免 #define(如 constexpr double PI = 3.14159;)。int 转 double 需显式 static_cast<double>(value))。enum class 替代普通枚举(强类型安全,如 enum class ImageFormat { JPEG, PNG, BMP };)。try-catch 捕获异常,避免返回错误码;自定义异常继承 std::exception(如 class ImageLoadError : public std::exception {};)。@throw std::runtime_error If image file not found)。cv::getTickCount() 移到循环外)。const(如 const cv::Mat& inputImage)减少拷贝,inline 函数优化短函数调用开销。std::vector 用 for (const auto& elem : vec),避免下标越界(vec.at(i) 替代 vec[i],越界抛异常)。delete 匹配 new。if (ptr) { /* 操作 */ }),释放后置 nullptr。if (kernelSize % 2 == 0) throw std::invalid_argument("Kernel size must be odd");)。cv::VideoCapture cap("video.mp4");,析构自动关闭)。clang-tidy 检测代码问题。ClangFormat 统一代码格式。Google Test / Catch2 编写测试用例,覆盖率不低于 **80%**,覆盖边界条件。feat: 增加图像直方图均衡化功能、fix: 修复图像旋转时的内存泄漏)。main(生产)、开发分支 develop,合并前运行测试。| 环节 | C++ 专项要求 | 通用原则 |
|---|---|---|
| 命名 | 宏全大写、枚举强类型(enum class) | 见名知意、无意义缩写 |
| 内存管理 | 智能指针优先、禁止裸指针泄漏 | 资源安全、避免冗余 |
| 语言特性 | constexpr、inline、RAII 机制 | 性能与可维护性平衡 |
| 错误处理 | 异常继承 std::exception、try-catch | 明确错误类型、可恢复性 |
| 工具 | ClangFormat、clang-tidy、Google Test | 自动化检测 + 格式化 |