Optional Property Handling in Objects: Readability vs Conciseness

3 Ways to Create an Object. Which camp are you in? Creating objects is something we do daily—whether it's for API payloads or state management. But the "right" way to handle optional properties often sparks a debate: Readability vs. Conciseness. Which pattern do you reach for most? 1️⃣ Procedural Assignment: The "old school" way. It's clear and performant, but can feel verbose as your object grows. 2️⃣ Object.assign: Functional, but often feels a bit dated. Plus, you have to be careful with null/undefined arguments. 3️⃣ The Conditional Spread: The modern favorite. It's concise and keeps everything inside the object literal, though it can look a bit "magical" to some. Which one do you prefer? Do you value the "clean" look of the spread operator, or do you prefer the explicit nature of a one-line if statement? Code snippets in the comments below. 👇 #TypeScript #JavaScript #WebDevelopment #CodeQuality #Frontend #CleanCode #CodingBestPractices #WebDev #CodingTips #itsmacr8

  • No alternative text description for this image

I think I only used method 3 because of React useState's property of not being mutable. Besides that, I'd always use method 1. It takes the least amount of cognitive load IMO

Like
Reply

I find myself gravitating toward conditional spread for everyday use, but when logic gets complex, that explicit procedural approach really helps future me understand what's happening.

Like
Reply
See more comments

To view or add a comment, sign in

Explore content categories