Empty Code Block

Incomplete Base
Structure: Simple
Description

An empty code block occurs when a section of source code, such as a conditional statement or function body, contains no executable statements.

Extended Description

Empty blocks can appear in various places like if/else conditionals, loops, function definitions, or exception handlers. While sometimes intentionally used as a placeholder, they often signal problems like incomplete features, accidentally deleted logic, or unexpected behavior from macros or code generators. For developers, these empty blocks are a red flag. Even if the language syntax allows them, they frequently violate API contracts or expected program behavior, leading to silent failures or security issues where an action is assumed but never executed. Code reviews should actively identify and justify any empty block to prevent unintentional gaps in application logic.

Common Consequences 1
Scope: Other

Impact: Reduce Reliability

Demonstrative Examples 2

ID : DX-213

In the following Java example, the code catches an ArithmeticException.

Code Example:

Bad
Java
java
Since the exception block is empty, no action is taken.
In the code below the exception has been logged and the bad execution has been handled in the desired way allowing the program to continue in an expected way.

Code Example:

Good
Java
java

ID : DX-214

The following code attempts to synchronize on an object, but does not execute anything in the synchronized block. This does not actually accomplish anything and may be a sign that a programmer is wrestling with synchronization but has not yet achieved the result they intend.

Code Example:

Bad
Java
java
Instead, in a correct usage, the synchronized statement should contain procedures that access or modify data that is exposed to multiple threads. For example, consider a scenario in which several threads are accessing student records at the same time. The method which sets the student ID to a new value will need to make sure that nobody else is accessing this data at the same time and will require synchronization.

Code Example:

Good
Java
java
Related Weaknesses