C & C++

C & C++

Made by DeepSource

Misleading indentation CXX-C2011

Style
Minor

Incorrect indentation and missing braces can lead to code that is difficult to read and understand which can result in developers spending more time identifying issues in the code.

Missing braces can also create ambiguity and make it harder to determine which statements are part of a conditional block.

It is recommended to always use braces, even when there is only one statement in the block. This helps to clearly define the scope of the conditional block and avoids potential issues.

Bad Practice

if (cond1)
  if (cond2)
    foo1();
else
  foo2();  // Wrong indentation: else belongs to if(cond2) statement.

if (cond1)
  foo1();
  foo2();  // Not guarded by if(cond1).

Recommended

if (cond1) {
  if (cond2) {
    foo1();
  }
} else { // braces clearly define where foo2 belongs
  foo2();
}

if (cond1)
  foo1();
foo2(); // correct indentation