Hot take: Optional chaining encourages writing brittle code
Hot take: Optional chaining encourages writing brittle code.
Example (currently a stage 1 JS feature):
const data = myObj?.firstProp?.secondProp?.actualData
Instead, normalize object structures at the time of creation (in JS apps, that often means in API call response handler)
Checking for property existence every time we need to use an object leads to needless noisy code, and confusion about data structures. Instead, when instantiating objects, make sure they have consistent properties.
Summary: Consistent object structures help avoid bugs.
For this reason, optional chaining is one of the rare proposed JS features I'm hoping won't end up in the language. I believe it encourages writing redundant and needlessly defensive code. Centralized normalization is cleaner, easier to understand, and easier to maintain.