Skip to content
Clean CodeChapter 7 / 57Intermediate6 min read

Conditionals Without Ternaries

Avoid nested ternaries for readability. Keep JSX conditions clean with if, early returns and render functions instead of ternaries and &&.

On this page

1. What is this practice?

This is one of the strictest house rules in this course. First, understand what it asks for:

PlaceRule
Normal codeif / else or an early return
Choosing a valueA lookup object or a small function
JSXA function like renderBanner() with a braced guard inside

2. Why does it matter?

A single ternary often looks fine. The trouble starts when it grows: a nested ternary, a long condition inside JSX, and the hidden bug of &&.

Ternary and && in JSXif and render functions
To read a nested ternary you have to build a table in your headEach case has its own line and its own guard
With count && Badge, if count is 0, a 0 appears on screenA clear boolean in the guard, no hidden 0
An 80-line JSX return block with conditions in the middleA short return block, every part has a name
Adding a case makes the ternary deeperA new case is just one more if
Hard to stop on one branch of a ternary in the debuggerEvery branch has its own line, so breakpoints are easy

The && bug is real:

tsx
<div>{cart.items.length && <CartBadge />}</div>

When the cart is empty, the result of 0 && ... is 0, and React writes 0 on the screen.

3. When to follow it

LevelHow importantWhy
Learning projectAvoid nested ternaries completely, a simple ternary is acceptableFirst make the habit of if and early return solid
Production codeFollow the team's rule, and never use a number with &&The 0 render bug and nested ternaries are the most common readability problems
Senior levelDecide how strict the team will beA clear rule ends arguments in review

4. Bad vs good code

tsx
export function OrderStatus({ order }: { order: Order }) {
  return (
    <div>
      {order.refunds.length && <RefundNote />}
      {order.status === "paid"
        ? order.shippedAt
          ? <Shipped at={order.shippedAt} />
          : <Preparing />
        : order.status === "failed"
          ? <PaymentFailed />
          : <Pending />}
    </div>
  );
}
tsx
export function OrderStatus({ order }: { order: Order }) {
  const renderRefundNote = () => {
    if (order.refunds.length === 0) {
      return null;
    }

    return <RefundNote />;
  };

  const renderState = () => {
    if (order.status === "failed") {
      return <PaymentFailed />;
    }

    if (order.status !== "paid") {
      return <Pending />;
    }

    if (!order.shippedAt) {
      return <Preparing />;
    }

    return <Shipped at={order.shippedAt} />;
  };

  return (
    <div>
      {renderRefundNote()}
      {renderState()}
    </div>
  );
}
What is the difference?BadGood
0 bugA 0 shows up when there are no refundsA clear === 0 guard
ReadingA 3-level nested ternaryGuards in a straight line from top to bottom
Return blockLogic and UI mixedOnly two named calls
A new statusThe ternary gets deeperOne new if

A lookup for choosing a value

ts
const STATUS_LABEL: Record<OrderStatus, string> = {
  pending: "Pending",
  paid: "Paid",
  failed: "Payment failed",
};

const label = STATUS_LABEL[order.status];

Because of Record, if you add a new status, TypeScript itself tells you the label is missing.

5. How to apply it

Steps to remove a ternary
Find the ternary or &&
Normal code? Change it to if/else or an early return
A value mapping? Make a lookup object
JSX? Make a renderX function
Inside the function, a guard with braces, return null
In the return block, only call renderX()

6. Exceptions and common mistakes

An honest note: most teams allow a simple ternary and &&. The React docs themselves show isLoggedIn ? <A /> : <B />. What almost everyone agrees on: do not write nested ternaries, and do not put a number on the left side of &&. "No ternaries at all" is a strict house opinion. The benefit: every condition has the same shape, there is no 0 bug, and JSX always stays short. The cost: 5 lines even for a tiny condition. A team that keeps this rule prefers consistency over short files.

If the render functions get too big, turn them into separate components. Render functions are for small conditions, not for whole sub-screens.

Mistake: && with a number in JSX

Why? 0 is falsy but React renders it as text, so a 0 appears on the screen.

Better: Make a clear boolean, like a length === 0 guard, or use a render function.

Mistake: Nested ternary

Why? The reader has to hold every level's condition in their head, which raises the chance of a mistake.

Better: Write a straight list of ifs and early returns.

Mistake: Big JSX and state inside a render function

Why? The render function turns into a hidden component, which makes re-rendering and testing harder.

Better: Move the big part into its own component file and pass props.

Mistake: A long if/else chain instead of a ternary when it is really a mapping

Why? Five ifs just to choose one string is noise.

Better: Use a lookup object typed with Record.

7. What to learn before and after

Final quick revision

CheckQuestion
Nested ternaryAny at all? Remove it right away
&& in JSXCould the left side be a number?
Render functionDoes every conditional piece of UI have a name?
GuardDoes the guard return null with braces?
MappingWould a lookup object be better?
Team ruleIs a simple ternary allowed or not, the same across the whole repo