backend-development

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Backend Development

后端开发

You are an expert in backend development with C++ and Elixir/Phoenix.
您是C++与Elixir/Phoenix领域的后端开发专家。

C++ Development

C++开发

Naming Conventions

命名规范

  • Use PascalCase for classes and structs
  • Use camelCase for variables and methods
  • Use SCREAMING_SNAKE_CASE for constants and macros
  • 类和结构体使用PascalCase命名
  • 变量和方法使用camelCase命名
  • 常量和宏使用SCREAMING_SNAKE_CASE命名

Memory Management

内存管理

  • Prefer smart pointers (
    std::unique_ptr
    ,
    std::shared_ptr
    ) over raw pointers
  • Use
    std::unique_ptr
    for exclusive ownership
  • Use
    std::shared_ptr
    only when shared ownership is required
  • Implement RAII for all resource management
  • Avoid unnecessary heap allocations
  • 优先使用智能指针(
    std::unique_ptr
    std::shared_ptr
    )而非裸指针
  • 独占所有权场景使用
    std::unique_ptr
  • 仅在需要共享所有权时使用
    std::shared_ptr
  • 所有资源管理都实现RAII机制
  • 避免不必要的堆内存分配

Modern C++ Features

现代C++特性

  • Use
    auto
    for type inference where it improves readability
  • Leverage range-based for loops
  • Use
    std::optional
    for values that may not exist
  • Use
    std::variant
    for type-safe unions
  • Apply structured bindings for cleaner code
  • Use
    std::move
    for move semantics
  • 在提升可读性的场景下使用
    auto
    进行类型推导
  • 利用基于范围的for循环
  • 使用
    std::optional
    处理可能不存在的值
  • 使用
    std::variant
    实现类型安全的联合体
  • 运用结构化绑定编写更简洁的代码
  • 使用
    std::move
    实现移动语义

Error Handling

错误处理

  • Use exceptions for error handling
  • Define custom exception types for domain-specific errors
  • Catch exceptions at appropriate boundaries
  • Ensure exception safety in all code
  • 使用异常进行错误处理
  • 针对领域特定错误定义自定义异常类型
  • 在合适的边界捕获异常
  • 确保所有代码具备异常安全性

Best Practices

最佳实践

  • Enforce const-correctness throughout
  • Avoid C-style casts; use
    static_cast
    ,
    dynamic_cast
    , etc.
  • Write unit tests with Google Test or Catch2
  • Document with Doxygen comments
  • 全程遵循const正确性
  • 避免C风格强制类型转换;使用
    static_cast
    dynamic_cast
    等替代
  • 使用Google Test或Catch2编写单元测试
  • 使用Doxygen注释编写文档

Elixir and Phoenix Best Practices

Elixir与Phoenix最佳实践

Core Philosophy

核心理念

  • Follow domain-driven design with PragDave philosophy
  • Use functional programming with explicit error handling
  • Embrace the "let it crash" principle
  • 遵循PragDave理念的领域驱动设计
  • 使用带有显式错误处理的函数式编程
  • 遵循“让它崩溃”的原则

Code Organization

代码组织

  • Organize code around business domains using Phoenix contexts
  • Keep contexts focused on single domains
  • Use bounded contexts to prevent coupling
  • Implement clear public APIs for each context
  • 使用Phoenix上下文围绕业务领域组织代码
  • 保持上下文聚焦于单一领域
  • 使用限界上下文避免耦合
  • 为每个上下文实现清晰的公共API

Pattern Matching and Control Flow

模式匹配与控制流

  • Use pattern matching extensively for data extraction
  • Apply "railway-oriented programming" with
    with
    statements
  • Chain operations cleanly with the pipe operator
  • Handle all pattern match cases explicitly
  • 大量使用模式匹配进行数据提取
  • 结合
    with
    语句应用“铁路导向编程”
  • 使用管道操作符清晰地链式调用操作
  • 显式处理所有模式匹配情况

Error Handling

错误处理

  • Return tagged tuples (
    {:ok, result}
    or
    {:error, reason}
    )
  • Use
    with
    statements to chain fallible operations
  • Implement proper supervision trees
  • Handle expected errors explicitly
  • 返回标记元组(
    {:ok, result}
    {:error, reason}
  • 使用
    with
    语句链式调用可能失败的操作
  • 实现合理的监督树
  • 显式处理预期错误

Phoenix Contexts

Phoenix上下文

  • Group related functionality in contexts
  • Define clear boundaries between contexts
  • Use contexts as the API layer for business logic
  • Keep controllers thin, delegate to contexts
  • 按功能相关性分组到上下文中
  • 定义上下文之间的清晰边界
  • 将上下文作为业务逻辑的API层
  • 保持控制器轻量化,将逻辑委托给上下文

LiveView

LiveView

  • Use LiveView as primary UI technology
  • Implement function components for reusable UI
  • Handle events in LiveView modules
  • Manage state appropriately in assigns
  • 将LiveView作为主要UI技术
  • 实现函数式组件以复用UI
  • 在LiveView模块中处理事件
  • 在assigns中合理管理状态

Data Validation

数据验证

  • Validate at boundaries using
    Ecto.Changeset
  • Use changesets even outside database contexts
  • Define clear validation rules
  • Return helpful error messages
  • 使用
    Ecto.Changeset
    在边界处进行验证
  • 即使在数据库上下文之外也使用changeset
  • 定义清晰的验证规则
  • 返回有帮助的错误提示信息