programming
if-condition
assignment
coding-tips
software-development

One line if-condition-assignment

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

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:

javascript
const label = isAdmin ? "Admin" : "User";
csharp
string label = isAdmin ? "Admin" : "User";

Python uses a different order:

python
label = "Admin" if is_admin else "User"

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:

python
status = "open" if tickets > 0 else "empty"

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:

javascript
const color = isSelected && isEnabled && !hasError ? "green" : isDisabled ? "gray" : "red";

Clearer:

javascript
1let color;
2
3if (isSelected && isEnabled && !hasError) {
4  color = "green";
5} else if (isDisabled) {
6  color = "gray";
7} else {
8  color = "red";
9}

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:

python
result = save() if should_save else log_skip()

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.

csharp
object value = useNumber ? 1 : "one";

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.

python
grade = "A" if score >= 90 else "B" if score >= 80 else "C"

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:

python
def status_label(count):
    return "ready" if count > 0 else "empty"

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:

  1. Can a teammate understand it in one pass
  2. 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 if blocks when logic becomes hard to scan.
  • Prefer clarity over clever compression every time.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.