Back to all reviewers

Use optional chaining operators

comfyanonymous/ComfyUI
Based on 2 comments
JavaScript

Use optional chaining (`?.`) and nullish coalescing (`??`) operators instead of manual null checks to safely access nested properties and provide fallback values. These operators prevent runtime errors when dealing with potentially null or undefined values and result in cleaner, more readable code.

Null Handling JavaScript

Reviewer Prompt

Use optional chaining (?.) and nullish coalescing (??) operators instead of manual null checks to safely access nested properties and provide fallback values. These operators prevent runtime errors when dealing with potentially null or undefined values and result in cleaner, more readable code.

Instead of verbose manual checks:

if (item.meta && item.meta[key] && item.meta[key].display_node) {
    app.nodeOutputs[item.meta[key].display_node] = value;
} else {
    app.nodeOutputs[key] = value;
}

Use optional chaining with nullish coalescing:

const realKey = item?.meta?.[key]?.display_node ?? key;
app.nodeOutputs[realKey] = value;

For simple property access, replace potentially unsafe access like input.widget.name with input.widget?.name to handle cases where the parent object might be null or undefined. This approach reduces boilerplate code while providing robust null safety.

2
Comments Analyzed
JavaScript
Primary Language
Null Handling
Category

Source Discussions