Back to all reviewers

Prefer nullish coalescing

langchain-ai/langchainjs
Based on 6 comments
TypeScript

When handling null or undefined values, use modern JavaScript patterns to write more robust and maintainable code: 1. Use the nullish coalescing operator (`??`) instead of logical OR (`||`) for defaults:

Null Handling TypeScript

Reviewer Prompt

When handling null or undefined values, use modern JavaScript patterns to write more robust and maintainable code:

  1. Use the nullish coalescing operator (??) instead of logical OR (||) for defaults: ```javascript // Good: Only falls back when value is null or undefined this.endpointUrl = fields.endpointUrl ?? getEnvironmentVariable(“AZUREML_URL”);

// Avoid: Will also replace empty strings, 0, and false this.endpointUrl = fields.endpointUrl || getEnvironmentVariable(“AZUREML_URL”);


2. Conditionally include properties in objects only when they exist:
```javascript
// Good: Only adds the property when content exists
const options = {
  ...otherOptions,
  ...(message.content != null ? { content: formatContent(message.content) } : {})
};

// Avoid: Passing undefined values to APIs
const options = {
  ...otherOptions,
  audio: this.audio // might be undefined
};
  1. Use strict equality (===, !==) for null checks:
    // Good: Clear intent, checks for both null and undefined
    if (documents === null || documents === undefined) {
      // Handle the case
    }
    // Or more concisely:
    if (documents == null) {
      // Handle the case
    }
    
  2. Thoroughly check combinations of conditions when handling nullable values: ```javascript // Good: Raises an error only when both client and credentials are missing if (!config.client && (!endpoint || !key)) { throw new Error(“Missing required configuration”); }

// Avoid: Oversimplified check if (!config.client && !endpoint && !key) { // This will only throw if ALL are missing } ```

These patterns help prevent subtle bugs and make your code’s intent clearer to other developers.

6
Comments Analyzed
TypeScript
Primary Language
Null Handling
Category