PAC-P008 · deep-nesting

Deeply nested conditions

info · Performance

if blocks nested more than two or three levels deep hide which rule decides. A flat sequence of if (...) return ...; statements gives first-match-wins semantics without nesting.

Why it matters

PAC logic is a priority list: the first rule that matches decides. Nested if/else blocks express the same thing with more state to keep in mind, and are where missing returns (PAC-X001) and shadowed branches (PAC-C014) hide. Beyond two levels, move the inner decision into a helper function with its own returns.

How to fix

Flatten into a sequence of if (...) { return ...; } statements; extract helper functions for sub-decisions.

Examples

Bad

function FindProxyForURL(url, host) {
  if (!isPlainHostName(host)) {
    if (!dnsDomainIs(host, ".corp.example")) {
      if (!shExpMatch(host, "*.lab.example")) {
        if (!(/^\d+\.\d+\.\d+\.\d+$/.test(host) && isInNet(host, "10.0.0.0", "255.0.0.0"))) {
          return "PROXY proxy.corp.example:8080";
        }
      }
    }
  }
  return "DIRECT";
}
Open bad example in checker

Good

function FindProxyForURL(url, host) {
  if (isPlainHostName(host)) { return "DIRECT"; }
  if (dnsDomainIs(host, ".corp.example")) { return "DIRECT"; }
  if (shExpMatch(host, "*.lab.example")) { return "DIRECT"; }
  if (/^\d+\.\d+\.\d+\.\d+$/.test(host) && isInNet(host, "10.0.0.0", "255.0.0.0")) { return "DIRECT"; }
  return "PROXY proxy.corp.example:8080";
}
Open good example in checker

Related rules