Readability Pitfalls
Keep expressions clear.
Power Can Hurt Readability
Ternaries are concise, but overusing them can make code cryptic.
The goal is clarity, not the fewest characters. A line a reader cannot parse at a glance is a bug waiting to happen.
This lesson covers common pitfalls and how to avoid them.
Pitfall: Deep Nesting
Stacking many ternaries on one line is hard to read and easy to get wrong.
Each extra level multiplies the mental effort needed to trace the logic.
// hard to follow on one line
String g = s>=90?"A":s>=80?"B":s>=70?"C":s>=60?"D":"F";All lessons in this course
- The Ternary Operator
- Nesting Ternaries
- Ternary vs if/else
- Readability Pitfalls