A flow-breaking structure interrupts straightforward top-to-bottom execution and makes readers track alternate execution paths. Cognitive complexity measures this mental effort rather than merely counting paths for testing. section 3.1.2
Common sources of cognitive load include:
- Loops, conditionals, exception handlers, mixed logical operators, and nesting interrupt the expected reading flow. section 3.1.2
- Nested flow-breaking structures compound the burden because readers must maintain more possible execution paths. section 3.1.2
- In Java scoring, each loop and each catch contributes one point; nesting increases the associated cognitive burden. section 3.1.2
Reminder-email validation
A reminder method combines a customer loop, active-customer calculation, several boolean conditions, email construction, and exception handling. Reading it requires following multiple possible paths, illustrating why nested flow-breaking structures raise cognitive complexity. section 3.1.2
Figure 3.2 Each flow-breaking structure (i.e., for, if, else) and each level of nesting increase cognitive complexity. In this example, the developer must keep track of several different execution paths to understand the overall logic.
Use the diagram to trace how loops, conditionals, and nesting multiply the execution paths a reader must keep in mind. section 3.1.2
