Back to all reviewers

Enforce null safety patterns

expressjs/express
Based on 8 comments
JavaScript

Implement comprehensive null safety patterns to prevent runtime errors and ensure predictable behavior. Follow these guidelines: 1. Use explicit null/undefined checks instead of loose comparisons:

Null Handling JavaScript

Reviewer Prompt

Implement comprehensive null safety patterns to prevent runtime errors and ensure predictable behavior. Follow these guidelines:

  1. Use explicit null/undefined checks instead of loose comparisons: ```javascript // Good if (value === undefined || value === null) { // handle null case }

// Avoid if (!value) // Could match 0, ‘’, false if (typeof value === ‘undefined’) // Doesn’t catch null


2. Never mutate input parameters or objects:
```javascript
// Good
function process(options) {
  const opts = { ...options }; // Create new object
  opts.value = opts.value || defaultValue;
  return opts;
}

// Avoid
function process(options) {
  options.value = options.value || defaultValue; // Mutates input
  return options;
}
  1. Use Object.create(null) for clean object instances without prototype chain: ```javascript // Good const cleanObject = Object.create(null);

// Avoid const dirtyObject = {}; // Inherits from Object.prototype


4. Implement strict type checking for boolean options:
```javascript
// Good
if (typeof options.flag !== 'boolean') {
  throw new TypeError('flag must be boolean');
}

// Avoid
if (options.flag) // Allows non-boolean values

This pattern prevents common null-related bugs, ensures type safety, and maintains immutability principles.

8
Comments Analyzed
JavaScript
Primary Language
Null Handling
Category

Source Discussions