0Pricing
Swift Academy · Lesson

Avoiding Nested Ternaries

Keep conditional logic readable and maintainable.

Avoiding Nested Ternaries is a free Swift Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

When Ternaries Help

A single ternary is clean and readable for a simple two-way choice. The trouble starts when you nest one ternary inside another.

let temp = 10
let label = temp < 0 ? "Freezing" : "Above zero"
print(label)

A Nested Ternary

You can nest ternaries to handle more than two cases, but the result quickly becomes hard to read.

let score = 75
let grade = score >= 90 ? "A" : score >= 80 ? "B" : score >= 70 ? "C" : "F"
print(grade)

Why Nesting Hurts

Nested ternaries force the reader to mentally track which colon pairs with which question mark. A small mistake silently changes behavior.

Prefer clarity over cleverness.

let value = 5
let text = value > 0 ? "positive" : value < 0 ? "negative" : "zero"
print(text)

Refactor to if/else if

The same multi-way decision reads much better as an if / else if / else chain. Each branch is on its own line.

let score = 75
let grade: String
if score >= 90 {
    grade = "A"
} else if score >= 80 {
    grade = "B"
} else if score >= 70 {
    grade = "C"
} else {
    grade = "F"
}
print(grade)

Using a switch Instead

For range-based decisions, a switch over ranges is often the cleanest option of all.

let score = 85
let grade: String
switch score {
case 90...100: grade = "A"
case 80..<90: grade = "B"
case 70..<80: grade = "C"
default: grade = "F"
}
print(grade)

Extract a Function

When a decision is reused, move it into a well-named function. The call site becomes self-documenting.

func gradeLetter(for score: Int) -> String {
    if score >= 90 { return "A" }
    if score >= 80 { return "B" }
    if score >= 70 { return "C" }
    return "F"
}
print(gradeLetter(for: 64))

One Ternary Is Fine

The rule is not "never use ternaries". A single, flat ternary for a clear binary choice is perfectly idiomatic Swift.

let isAdmin = true
let role = isAdmin ? "Admin" : "User"
print(role)

Readability Over Brevity

Shorter code is not always better code. If a reviewer has to pause to decode a line, prefer the longer, clearer form.

let stock = 0
let availability = stock > 0 ? "In stock" : "Sold out"
print(availability)

Avoid Side Effects in Ternaries

Keep both branches of a ternary to simple values. Hiding function calls with side effects inside a ternary makes code surprising.

let level = 2
let points = level == 1 ? 100 : 200
print(points)

Combining With Named Conditions

If a condition is complex, store it in a named Bool first, then use a single ternary. The name explains the intent.

let age = 20
let hasLicense = true
let eligible = age >= 18 && hasLicense
let result = eligible ? "Approved" : "Denied"
print(result)

A Clear Multi-Way Example

Compare this readable if-chain with the earlier nested ternary. Both produce the same answer, but this one is easy to maintain.

let value = -3
let sign: String
if value > 0 {
    sign = "positive"
} else if value < 0 {
    sign = "negative"
} else {
    sign = "zero"
}
print(sign)

Quick Check

Choose the best practice.

Recap: Avoiding Nested Ternaries

Use a single flat ternary for binary choices. For three or more cases, reach for if/else if or switch. Prioritize readability so your future self can follow the logic.

let n = 7
let parity = n % 2 == 0 ? "even" : "odd"
print(parity)

Frequently asked questions

Is the “Avoiding Nested Ternaries” lesson free?

Yes — the full text of “Avoiding Nested Ternaries” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.

What will I learn in “Avoiding Nested Ternaries”?

Keep conditional logic readable and maintainable. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Swift Academy?

No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Avoiding Nested Ternaries” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Swift Academy lesson?

Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Bool Values and Expressions
  2. The Ternary Conditional Operator
  3. Avoiding Nested Ternaries
  4. Boolean Methods and Toggling
← Back to Swift Academy