📎 Webclip
Code Smell 149 — Optional Chaining
The page treats optional chaining as a code smell because it keeps nulls and undefined in the code path. Its position is that mature, robust code should remove nulls and avoid the need for optional access or explicit undefined checks.
Reading notes#
- Optional chaining is presented as a way to hide null under the rug rather than solve the underlying problem.
- The page says nulls and undefined should be removed, and then optionals are no longer needed.
- It groups optional chaining with other ways of dealing with nullish values, such as optionals and coalescence.
- In the sample code,
user?.credentials?.notExpiredis marked wrong because it depends on potential nulls and undefined. - The right example uses a real user object or a polymorphic
NullUser, with credentials always defined. - An explicit
!== undefinedcheck is also labeled wrong, because explicit undefined checks are another code smell. - The page says this is an automatic, language-feature-level smell that can be detected and removed.
- It also connects the topic to null, short circuit hacks, big bang castings, and the broader issue of nullish values.
