AI summary by TwinMind

We Need To Talk About Ternaries

Theo - t3․gg

19m 9s

JavaScript Ternary Operators Overview

  • Ternary operators (? :) cause anxiety in JavaScript development
  • Single or double question marks create unpredictable behavior
  • Ternaries often used/abused with nested patterns causing problems
  • Important to use good patterns for these operators

Kent's PayPal Bug Example

  • Article discusses && operator issues in JSX
  • Problem: contacts.length && <ContactList /> renders "0" when contacts.length is 0
  • && checks for falsy values, returns 0 instead of empty list
  • Solution shown: ternary contacts.length ? <ContactList /> : null
  • Better solution: force boolean with !!contacts.length && <ContactList />

Nested Ternaries Readability Issues

  • Demonstrated with sidebar/user info example
  • Nested ternaries become unreadable quickly
  • Control flow unclear - reading left-to-right vs top-to-bottom
  • Code diffs become unparsable in code reviews
  • Example: sidebarOpen ? <div>sidebar {userInfoOpen ? <div>user info</div> : null}</div> : null

Component-Based Solution

  • Break out separate components instead of nested ternaries
  • Created Sidebar and UserInfo components
  • Each component handles own conditional logic with early returns
  • if (!sidebarOpen) return null pattern
  • Explicit dependency chains through props
  • Much clearer top-to-bottom reading flow
  • Easy to locate where changes need to be made

When Ternaries Are Necessary

  • Only necessary in TypeScript type definitions
  • No if statements available in TypeScript types
  • Example: Flatten function type definition
  • T extends any[] ? T[number] : T pattern
  • Used for conditional type checking and extraction
  • Complex nested ternaries in library code (tRPC examples)

tRPC Complexity Example

  • Showed deprecated tRPC type definitions
  • Extremely complex nested ternaries with multiple any types
  • Order of conditions matters significantly
  • Code becomes unparsable even for original authors
  • Necessary evil for complex type definitions
  • More common in library code than application code

Prettier's New Ternary Formatting

  • New experimental formatting for better ternary readability
  • Addresses historical formatting problems since 2018
  • Criteria: easy to follow if/then/else, fluid scaling, works in JSX/TS/JS
  • New syntax: question marks at line end, colons at line start
  • Lines without either are "then" conditions
  • Provides match-style syntax approach

Prettier Formatting Examples

  • pet.canBark ? "dog" : pet.canMeow ? "cat" : "unknown"
  • Improved readability over traditional nested ternaries
  • Beta testers initially skeptical but adapted quickly
  • Still not recommended for regular JavaScript control flow
  • Primarily beneficial for TypeScript type definitions

Final Recommendations

  • Avoid nested ternaries in regular JavaScript/JSX
  • Break out components with early returns instead
  • Use explicit if statements for better readability
  • Top-to-bottom reading flow test for maintainable code
  • Ternaries acceptable only in TypeScript type definitions
  • Component extraction preferred over inline conditional logic

TwinMind

Get summaries like this for your own meetings

TwinMind takes notes in the background during meetings, lectures, and calls, then writes the summary for you.

Free plan to get started

No bot joins your calls

Phone, desktop & Chrome apps

Your next meeting, summarized like this

TwinMind listens in the background and turns meetings, lectures, and calls into searchable notes you can chat with. Start free.

Available on phone, desktop & Chrome

Your next meeting, summarized like this

TwinMind listens in the background and turns meetings, lectures, and calls into searchable notes you can chat with. Start free.

Available on phone, desktop & Chrome

Your next meeting, summarized like this

TwinMind listens in the background and turns meetings, lectures, and calls into searchable notes you can chat with. Start free.

Available on phone, desktop & Chrome