Back to all reviewers

null checks before operations

denoland/deno
Based on 2 comments
TypeScript

Always check for null or undefined values before performing operations on objects, accessing properties, or calling methods. This prevents runtime errors that occur when attempting to operate on null/undefined values.

Null Handling TypeScript

Reviewer Prompt

Always check for null or undefined values before performing operations on objects, accessing properties, or calling methods. This prevents runtime errors that occur when attempting to operate on null/undefined values.

When working with potentially nullable objects, add explicit null checks before accessing properties or calling methods:

// Before - can throw if response is null
if (typeof response === "object" && ReflectHas(response, "then")) {
  // ...
}

// After - safe with null check
if (response !== null && typeof response === "object" && ReflectHas(response, "then")) {
  // ...
}

Similarly, check for property existence before using them:

// Check if optional properties exist before use
if (span) {
  span.recordException(err);
  if (err.name) {  // Check err.name exists before using
    span.setAttribute("error.type", err.name);
  }
}

This pattern is especially important when calling methods like Reflect.has(), Object.keys(), or accessing properties on objects that might be null or undefined. The small overhead of these checks prevents difficult-to-debug runtime errors.

2
Comments Analyzed
TypeScript
Primary Language
Null Handling
Category

Source Discussions