How we misuse naming boolean and how they should be named

Shameel Uddin - Nov 28 '23 - - Dev Community

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

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player