大多数时候,空的代码块意味着真的有代码忘写了。所以这些空的代码块必须补全或者应该直接删除。 如果代码块了注释,它就不会被认为是空的了。

下列代码演示了这个规则:

void doSomething() {
  int myVar = 0;
  for (int i = 0; i< 4; i++) // Non-Compliant
  {
  }
  for (int i = 0; i< 4; i++); // Compliant as there is no block

  if (myVar == 4) // Compliant as the block contains at least a comment
  {
    // nothing to do
  }
  else // Compliant
  {
    doSomething();
  }

  try // Non-Compliant
  {
  }
  catch (Exception e) // Non-Compliant
  {
  }
}