| 1 |
.. title:: clang-tidy - readability-avoid-unconditional-preprocessor-if |
| 2 |
|
| 3 |
readability-avoid-unconditional-preprocessor-if |
| 4 |
=============================================== |
| 5 |
|
| 6 |
Finds code blocks that are constantly enabled or disabled in preprocessor |
| 7 |
directives by analyzing ``#if`` conditions, such as ``#if 0`` and ``#if 1``, |
| 8 |
etc. |
| 9 |
|
| 10 |
.. code-block:: c++ |
| 11 |
|
| 12 |
#if 0 |
| 13 |
// some disabled code |
| 14 |
#endif |
| 15 |
|
| 16 |
#if 1 |
| 17 |
// some enabled code that can be disabled manually |
| 18 |
#endif |
| 19 |
|
| 20 |
Unconditional preprocessor directives, such as ``#if 0`` for disabled code |
| 21 |
and ``#if 1`` for enabled code, can lead to dead code and always enabled code, |
| 22 |
respectively. Dead code can make understanding the codebase more difficult, |
| 23 |
hinder readability, and may be a sign of unfinished functionality or abandoned |
| 24 |
features. This can cause maintenance issues, confusion for future developers, |
| 25 |
and potential compilation problems. |
| 26 |
|
| 27 |
As a solution for both cases, consider using preprocessor macros or defines, |
| 28 |
like ``#ifdef DEBUGGING_ENABLED``, to control code enabling or disabling. |
| 29 |
This approach provides better coordination and flexibility when working with |
| 30 |
different parts of the codebase. Alternatively, you can comment out the entire |
| 31 |
code using ``/* */`` block comments and add a hint, such as ``@todo``, |
| 32 |
to indicate future actions. |