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
SidebarandUserInfocomponents - Each component handles own conditional logic with early returns
if (!sidebarOpen) return nullpattern- 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
ifstatements available in TypeScript types - Example: Flatten function type definition
T extends any[] ? T[number] : Tpattern- 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
anytypes - 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
