if statement without braces
low
if (cond) return "DIRECT"; works, but the next person who adds a line under it changes the logic without noticing. PAC files are edited in production by people who are not JavaScript developers; braces are cheap insurance.
Why it matters
Without braces only the first statement belongs to the if. Adding a log line, a
variable assignment or a second return later silently moves the original statement out of
the condition or leaves the new one unconditional. The pattern is responsible for a whole
class of “it worked until we added one line” incidents. Consistent braces also make the
structure visible in diffs.
How to fix
Wrap every if/else body in braces.
Examples
Bad
function FindProxyForURL(url, host) {
if (isPlainHostName(host))
return "DIRECT";
return "PROXY proxy.corp.example:8080";
}
Open bad example in checkerGood
function FindProxyForURL(url, host) {
if (isPlainHostName(host)) {
return "DIRECT";
}
return "PROXY proxy.corp.example:8080";
}
Open good example in checkerRelated rules
- Deeply nested conditions PAC-P008