Skip to main content

Four Java Bugs That Bad Indentation Hides in Plain Sight

Discover four sneaky Java bugs where misleading code indentation and misplaced braces compile successfully but trick developers.

AI-written
Inewgen
28 Aug 2026Source: Dev.to3 min read (0 views)Last updated 19 Sep 2026
Share
Four Java Bugs That Bad Indentation Hides in Plain Sight

Stock photo for illustration only, not from the actual event

Font size
  • Java compiler completely ignores whitespace and code layout, focusing purely on token streams.
  • Misleading indentation makes human readers trust visual alignment over actual brace logic.
  • Historical bugs like Apple's goto fail prove that unbraced guards can bypass security checks.
  • Enforcing format on save and CI build checks stops code drift before production.

In software development, the javac compiler does not care what your code looks like. Whitespace carries zero meaning in the Java Language Specification, meaning the compiler reads your file as a stream of tokens and skips every space you type. This works fine for machines, but creates a major problem for humans because code indentation is the primary way developers communicate with each other. When the visual layout says one thing and the actual braces say another, the human reader invariably trusts the layout, and the bug ships.

Here are four bugs the author has either shipped, reviewed, or nearly missed. Every single snippet compiles correctly, and in every case, the indentation is actively lying to the reader.

  • The trap hidden inside for and while loops: a line like for (Item item : items); features a semicolon that makes the loop iterate over everything and do nothing, while the indented line below it runs only once.
  • Nested if-else ambiguity: writing statements like if (customer.isVip()) if (order.isGiftWrapped()) includeGiftCard(order); else chargeUpgradeFee(order); causes Java to bind the else clause to the nearest if statement, incorrectly charging upgrade fees to VIP customers without gift wraps.
  • Refactoring fallout: when someone deletes a line of code, closing braces drift up by one row without anyone re-formatting the block.
  • The famous ancestor bug CVE-2014-1266, Apple's goto fail bug in C, where a duplicated line under an unbraced if chain bypassed SSL certificate verification on iOS devices for over a year.
java programming code editor screen

Stock photo for illustration only, not from the actual event

The fact that code with severely misaligned indentation still compiles highlights a critical limitation of relying solely on human code reviews. Developers under tight deadlines read code less carefully, meaning visual anomalies easily slip past unnoticed. Static analysis tools like SonarQube rule S1116 can catch these patterns, but only if executed. The ultimate lesson is that layout management should never be left to manual human re-indentation under pressure.

To permanently eliminate code drift and formatting bugs, engineering teams should rely on automated tools rather than human discipline. Implementing strict automated checks ensures code consistency from development workstations all the way to production deployment.

Three core habits offer the highest impact for maintaining clean codebases:

  • Format on save: configure IntelliJ save actions or VS Code save hooks so files stay in canonical shape instantly.
  • Enforce format checks in CI: use Spotless, Checkstyle, or fmt-maven-plugin to fail builds when formatting drifts.
  • Use quick formatters: paste review snippets into JavaFmt, a browser-based tool that applies standard four-space formatting securely.

Source: Dev.to

Comments

Leave a Comment
0/2000

Found something wrong in this article? Report an issue with this article