openharmony-cpp

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

OpenHarmony C++ Coding Skills

OpenHarmony C++编码技能

Core Mandates (Common Pitfalls)

核心规则(常见陷阱)

These rules are strictly enforced in OpenHarmony and often differ from general C++ projects.
这些规则在OpenHarmony中被严格执行,且通常与通用C++项目有所不同。

1. Strict Naming & Formatting

1. 严格的命名与格式规范

  • Extensions: Always use
    .cpp
    and
    .h
    .
  • Files: Filenames must match class names (Unix-like, e.g.,
    my_class.cpp
    ).
  • Variables: Global vars must start with
    g_
    (e.g.,
    g_config
    ). Class members must end with
    _
    (e.g.,
    value_
    ).
  • Braces: K&R Style is mandatory (opening brace on the same line).
  • Details: See naming_formatting.md.
  • 扩展名: 始终使用
    .cpp
    .h
  • 文件: 文件名必须与类名匹配(类Unix风格,例如
    my_class.cpp
    )。
  • 变量: 全局变量必须以
    g_
    开头(例如
    g_config
    )。类成员变量必须以
    _
    结尾(例如
    value_
    )。
  • 大括号: 强制使用K&R风格(左大括号与代码行在同一行)。
  • 详情: 参见naming_formatting.md

2. Header Management

2. 头文件管理

  • Guards: Use
    #ifndef
    guards. #pragma once is FORBIDDEN.
  • Includes: Prefer
    #include
    over forward declarations to prevent hidden dependencies.
  • Details: See headers_scopes.md.
  • 防护: 使用
    #ifndef
    防护。禁止使用#pragma once。
  • 包含: 优先使用
    #include
    而非前向声明,以避免隐藏依赖。
  • 详情: 参见headers_scopes.md

3. Critical Security Requirements

3. 关键安全要求

  • Input Validation: All external data must be validated before use (indices, sizes, loops).
  • Memory Safety: Pointers must be set to
    nullptr
    immediately after deletion.
  • Bitwise: Only on unsigned types.
  • Details: See secure_coding.md.
  • 输入验证: 所有外部数据在使用前必须经过验证(索引、大小、循环)。
  • 内存安全: 指针在释放后必须立即设置为
    nullptr
  • 位操作: 仅允许对无符号类型进行位操作。
  • 详情: 参见secure_coding.md

Usage Guide

使用指南

When to use

使用场景

  • New Code: Generating new classes or modules.
  • Refactoring: Cleaning up legacy code.
  • Review: Checking code against OpenHarmony standards.
  • 新代码: 生成新类或模块。
  • 重构: 清理遗留代码。
  • 审核: 检查代码是否符合OpenHarmony标准。

Reference Map

参考文档映射

  • naming_formatting.md: Naming conventions (g_, _), braces (K&R), line length.
  • secure_coding.md: Input validation, integer safety, memory management, prohibited functions.
  • class_function_design.md: Constructors, inheritance, modern C++ (nullptr, const), function size.
  • headers_scopes.md: Header guards, namespaces, include rules.
  • naming_formatting.md: 命名规范(g_, _)、大括号(K&R)、行长度。
  • secure_coding.md: 输入验证、整数安全、内存管理、禁用函数。
  • class_function_design.md: 构造函数、继承、现代C++(nullptr、const)、函数大小。
  • headers_scopes.md: 头文件防护、命名空间、包含规则。