How we misuse naming boolean and how they should be named

shameel

Shameel Uddin

Posted on November 28, 2023

How we misuse naming boolean and how they should be named

Boolean is a pretty cool data type which we often use in our conditionals.
The blog explains how they should be named properly for the sake of clean coding practices in our codebase.

Positive over Negative

Banish the confusion of double negatives! Instead of drowning in a sea of "not," embrace clarity.

❌ if (!isNotUser)

✅ if (isUser)

Adjectives, Not Actions

Break free from mundane verbs and nouns! Elevate your boolean game with descriptive adjectives or short, snappy phrases.

❌ if (sendEmail)

✅ if (emailIsSent)

Present Tense

Time-traveling through tenses adds unnecessary noise. Stick to the present tense for clean, coherent code.

❌ if (hasBeenSubscribed)

✅ if (isSubscribe)

Proper use of Prefix

Boost readability with prefixes like is/has/should/can. Your code will thank you later!

❌ if (membership)

✅ if (hasMembership)

Avoid confusing true/false - Employ Enums!

Wave goodbye to mysterious true/false values. Embrace the power of enums for self-documenting code.

❌ bookMeeting(customer, true)

❌ bookMeeting(customer, false)

✅ bookMeeting(customer, Discount.Applied)

✅ bookMeeting(customer, Discount.NotApplied)

Happy coding! 🎉💻✨

Follow me for more such content:
LinkedIn: https://www.linkedin.com/in/shameeluddin/
Github: https://github.com/Shameel123

💖 💪 🙅 🚩
shameel
Shameel Uddin

Posted on November 28, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Beginner's TypeScript #2
programming Beginner's TypeScript #2

December 19, 2023

What is a Type Guard. TypeScript
typescript What is a Type Guard. TypeScript

June 9, 2022