Type-appropriate null representation

Always use type-appropriate mechanisms to represent the absence of a value. For objects, use default constructors rather than nullptr. For potentially absent values, consider using optional types. For pointers, use null checks before performing operations on them.

copy reviewer prompt

Prompt

Reviewer Prompt

Always use type-appropriate mechanisms to represent the absence of a value. For objects, use default constructors rather than nullptr. For potentially absent values, consider using optional types. For pointers, use null checks before performing operations on them.

Examples:

  • INCORRECT: bias_md = nullptr; when bias_md is an object type (dnnl::memory::desc)
  • CORRECT: bias_md = dnnl::memory::desc(); to represent an “empty” descriptor

  • INCORRECT: ncclHeartbeatMonitorThread_ = std::thread(...); without checking current state
  • CORRECT:
    // Check nullness before assignment to prevent issues on second call
    if (!ncclHeartbeatMonitorThread_.joinable()) {
    ncclHeartbeatMonitorThread_ = std::thread(...);
    }
    

This practice improves type safety, prevents null pointer dereferences, and creates more maintainable code by using language features designed for representing absence of values.

Source discussions