CodeQL documentation

Block with too many statements

ID: cpp/complex-block
Kind: problem
Severity: recommendation
Precision: high
Tags:
   - testability
   - readability
   - maintainability
Query suites:
   - cpp-security-and-quality.qls

Click to see the query in the CodeQL repository

This rule finds blocks of code that have too many complex statements, such as branching statements (if, switch), and loops (for, while).

Blocks with too many consecutive statements are candidates for refactoring. Only complex statements are counted here (eg. for, while, switch …). The top-level logic will be clearer if each complex statement is extracted to a function.

Recommendation

It is often the case that each consecutive complex statement performs a dedicated separate task. It is a very common case that each complex statement is actually commented with a description of the task. Extract each such task into its own function for improved readability and to promote reuse.

References