New Tool Radar

AI for Data & Spreadsheets

How to Check Whether an AI-Written Nested IF Can Be Rewritten as IFS or SWITCH

Check whether an AI-written nested IF can be rewritten as IFS or SWITCH so the logic is easier to audit.

Schematic with three steps: Count the nesting levels and list the; Rewrite the logic as IFS or SWITCH; Test both versions with every branch.

A nested IF tests one condition, then another inside its false branch, then another, and after three or four levels the parentheses and the order of the tests become hard to follow. IFS lists the tests in order without nesting, and SWITCH matches one value against a list of cases. An AI-written nested IF that works is still worth rewriting when a reader must audit it later. Check the depth and the pattern before keeping it.

What you need

  • The AI-written nested IF formula
  • The conditions it tests, listed in order
  • A test value for each branch, with the expected result

Step 1: Count the nesting levels and list the tests

Read the formula and count how many IFs sit inside each other. Write each test and its result in a small table, in the order the formula checks them.

Step 2: Rewrite the logic as IFS or SWITCH

If the tests are a series of conditions on different values, rewrite them as IFS with the same tests in the same order. If they all compare one value with several fixed values, rewrite them as SWITCH.

Step 3: Test both versions with every branch

Run the original and the rewrite on a test value for each branch, including the fallback case. Every branch must return the same result in both versions.

Worked example: synthetic grade bands

The table below is a synthetic example. It shows the expected result of checking an AI-written nested IF; no formula was run.

Check Value Result
Nested IF levels 4 Four IFs deep
Tests in order A>=90, A>=80, A>=70 Grade bands
IFS rewrite Same three tests No nesting
Test value 85 B Both versions return B

Expected result (UNTESTED). the nested IF with four levels and the IFS rewrite both return B for a score of 85, and every other branch agrees; the IFS version is kept because its tests read in order without nesting. This check was not run live; the table shows the expected result for the synthetic example.

Limits

  • IFS needs Excel 2019 or later, or Microsoft 365; older versions show #NAME?.
  • IFS stops at the first true test, so the order of the tests decides the result, just as in a nested IF.
  • SWITCH matches one value against fixed cases and needs a default value for anything unmatched.
  • A nested IF that returns different types from different branches can hide a type error that IFS would show in the same way.

Sources

See all