返回 Skill 列表
extension
分类: 开发与工程无需 API Key

numeric-data-processing-standards

提供了处理代码中数值数据的全面指南,包括数据类型选择、避免使用魔法数、类型转换以及边界检查。在编写或审查涉及数值变量、常量、计算或比较的代码时,请使用此技能以防止类型错误、溢出和运行时异常。

person作者: jakexiaohubgithub

Numeric Data Processing Standards

When to Use

Apply this skill when:

  • Defining numeric variables or constants
  • Performing numeric calculations or operations
  • Comparing numeric values
  • Converting between data types
  • Reviewing code for potential numeric-related defects

Core Procedures

1. Data Type Selection and Declaration

  • Choose appropriate data type based on requirements (BCD, floating-point, integer)
  • Review compiler warnings related to data types
  • Add explanatory comments when declaring variables
  • Define portable data types for cross-platform compatibility

2. Literals and Magic Number Handling

  • Replace literal numbers with named constants
  • Avoid hardcoded 0 and 1 except when semantically appropriate
  • Eliminate "magic numbers" by using descriptive constant names

3. Numeric Operations and Conversions

  • Handle numeric type conversions explicitly
  • Avoid comparisons between mixed types
  • Prevent operations on numbers with vastly different magnitudes
  • Consider computational cost of common operations

4. Error Prevention and Boundary Checks

  • Implement integer overflow protection
  • Validate integer ranges before operations
  • Prevent division by zero errors
  • Apply boundary analysis to avoid off-by-one errors

Key Considerations

  • Type Safety: Always verify type compatibility before operations
  • Precision: Be aware of floating-point precision limitations
  • Portability: Use type definitions that work across platforms
  • Performance: Balance safety with computational efficiency
  • Readability: Named constants improve code maintainability

Common Pitfalls to Avoid

  • Implicit type conversions that lose precision
  • Comparing floating-point numbers for exact equality
  • Assuming integer overflow behavior across compilers
  • Using magic numbers without documentation
  • Ignoring compiler warnings about type mismatches