Rule A2-7-2 (required, implementation, non-automated)

Sections of code shall not be “commented out”.

Rationale

Comments, using both C-style and C++ comments, should only be used to explain aspect of the source code. Code that is commented-out may become out of date, which may lead to confusion while maintaining the code.

Additionally, C-style comment markers do not support nesting, and for this purpose commenting out code is dangerous, see: M2-7-1. Note that the code that is a part of a comment (e.g. for clarification of the usage of the function, for specifying function behavior) does not violate this rule. As it is not possible to determine if a commented block is a textual comment, a code example or a commented-out piece of code, this rule is not enforceable by static analysis tools.

Example

// $Id: A2-7-2.cpp 305382 2018-01-26 06:32:15Z michal.szczepankiewicz $
#include <cstdint>
void Fn1() noexcept
{
std::int32_t i = 0;

/*
//
* ++i; /* incrementing the variable i */
//
/
*
//
// Non-compliant - C-style comments nesting is not supported,
//
compilation error
for (; i < 10; ++i)
{
// ...
}
}
void Fn2() noexcept
{
std::int32_t i = 0;
// ++i; // Incrementing the variable i // Non-compliant - code should not
// be commented-out
for (; i < 10; ++i)
{
// ...
}
}
void Fn3() noexcept
{
std::int32_t i = 0;
++i; // Incrementing the variable i using ++i syntax // Compliant - code
// is not commented-out, but ++i occurs in a
// comment too
for (; i < 10; ++i)
{
// ...
}
}

See also

MISRA C++ 2008 [7]: Rule 2-7-2 Sections of code shall not be “commented out” using C-style comments.

MISRA C++ 2008 [7]: Rule 2-7-3 Sections of code should not be “commented out” using C++ comments.