Back to all reviewers

Use explicit optional types

apache/mxnet
Based on 3 comments
Other

When dealing with values that may be absent or null, always use explicit optional type wrappers instead of implicit null checks or nullable types without clear type constraints. This makes the code's intent clearer, prevents null reference errors, and improves type safety.

Null Handling Other

Reviewer Prompt

When dealing with values that may be absent or null, always use explicit optional type wrappers instead of implicit null checks or nullable types without clear type constraints. This makes the code’s intent clearer, prevents null reference errors, and improves type safety.

In Perl, use Maybe[Type] to clearly indicate a value could be absent:

has 'sum_metric' => (is => 'rw', isa => 'Maybe[Num|ArrayRef[Num]|PDL]');

In C++, use appropriate optional containers like dmlc::optional<T> (or std::optional<T> in C++17) when a parameter might not have a value:

dmlc::optional<bool> shifted_output;  // Clearly indicates this boolean might not be set

Be explicit about which values can be null and provide appropriate methods to safely access or transform these values. When working with optional types, ensure consistent handling across the codebase rather than using ad-hoc null checks. This pattern helps catch null-related errors at compile time rather than runtime.

3
Comments Analyzed
Other
Primary Language
Null Handling
Category

Source Discussions