One line if-condition-assignment
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
One-line conditional assignment is usually implemented with a ternary operator or an equivalent expression form. It is useful when the condition and both outcomes are simple enough to read at a glance. Used well, it reduces boilerplate. Used badly, it compresses complex branching into unreadable code.
Basic Form Across Languages
Many languages use a ternary operator:
Python uses a different order:
The concept is the same: choose one expression if the condition is true and another if it is false.
Good Use Cases
One-line assignment works best when:
- both outcomes are short
- the condition is easy to read
- there is no side effect in either branch
Example:
This is usually clearer than a full multi-line if statement.
When a Full if Is Better
If either branch does real work or the condition is complex, expand it.
Harder to read:
Clearer:
Ternary expressions are for simple selection, not for compressing decision trees.
Avoid Side Effects in Branches
A conditional assignment should normally return values, not trigger behavior.
Less ideal:
This mixes action and assignment, which makes code review harder. Prefer explicit statements when branch behavior matters.
Type and Readability Considerations
In strongly typed languages, one-line assignment also needs compatible branch types.
This compiles in some contexts but may weaken clarity. Even when legal, mixed-type ternaries can make downstream code harder to reason about.
Chaining and Nesting
Nested ternaries are legal in many languages, but should be rare.
This can still be acceptable for tiny classification logic, but it becomes fragile fast. Once the logic requires comments or mental backtracking, use normal branching instead.
Assignment Versus Expression Return
One-line conditional assignment is often confused with general conditional expressions returned from functions. For example:
This is still fine because the expression is short and the function intent is obvious. The same readability standard applies whether the result is assigned to a variable or returned directly.
Expression-Oriented Code Can Be Useful
Some code styles prefer expressions because they compose well, especially in functional-style pipelines or template logic. That is fine as long as readability is preserved.
The real standard is not “one line is better” but “the intent is obvious.”
Practical Style Rule
Use one-line conditional assignment when you can answer yes to both:
- Can a teammate understand it in one pass
- Would expanding it to multiple lines add no real clarity
If either answer is no, use an ordinary if.
Team Style and Consistency
Consistency matters more than personal cleverness. If a codebase generally uses simple ternaries for short value choices, follow that style. If the team prefers explicit branching for business rules, align with that instead of optimizing for fewer lines.
Common Pitfalls
- Using nested ternaries for complex decision trees.
- Mixing assignment and side-effect-heavy function calls in one conditional expression.
- Forcing one-line style where multi-line branching is clearer.
- Writing branches with incompatible or confusing value types.
- Treating terseness as a design goal instead of readability.
Summary
- One-line conditional assignment is useful for simple value selection.
- Ternary syntax varies by language, but the design tradeoff is the same.
- Keep conditions and results short and side-effect free.
- Expand to normal
ifblocks when logic becomes hard to scan. - Prefer clarity over clever compression every time.
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.