Extra brace brackets in C++ code

Viewed 16697

Sometimes you run into code that has extra brace brackets, that have nothing to do with scope, only are for readability and avoiding mistakes.

For example:

GetMutexLock( handle ) ; 
{
  // brace brackets "scope" the lock,
  // must close block / remember
  // to release the handle.
  // similar to C#'s lock construct
}
ReleaseMutexLock( handle ) ;

Other places I have seen it are:

glBegin( GL_TRIANGLES ) ;
{
  glVertex3d( .. ) ;
  glVertex3d( .. ) ;
  glVertex3d( .. ) ;
} // must remember to glEnd!
glEnd() ; 

This introduces a compiler error if the mutex isn't freed (assuming you remember both the } and the Release() call).

  1. Is this a bad practice? Why?
  2. If it isn't one, could it change how the code is compiled or make it slower?
8 Answers
Related