Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1040011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:10:16+00:00 2026-05-16T15:10:16+00:00

Sometimes you run into code that has extra brace brackets, that have nothing to

  • 0

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?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T15:10:17+00:00Added an answer on May 16, 2026 at 3:10 pm

    The braces themselves are fine, all they do is limit scope and you won’t slow anything down. It can be seen as cleaner. (Always prefer clean code over fast code, if it’s cleaner, don’t worry about the speed until you profile.)


    But with respect to resources it’s bad practice because you’ve put yourself in a position to leak a resource. If anything in the block throws or returns, bang you’re dead.

    Use Scope-bound Resource Management (SBRM, also known as RAII), which limits a resource to a scope, by using the destructor:

    class mutex_lock
    {
    public:
        mutex_lock(HANDLE pHandle) :
        mHandle(pHandle)
        {
            //acquire resource
            GetMutexLock(mHandle);
        }
    
        ~mutex_lock()
        {
            // release resource, bound to scope
            ReleaseMutexLock(mHandle);
        }
    
    private:
        // resource
        HANDLE mHandle;
    
        // noncopyable
        mutex_lock(const mutex_lock&);
        mutex_lock& operator=(const mutex_lock&);
    };
    

    So you get:

    {
      mutex_lock m(handle);
      // brace brackets "scope" the lock,
      // AUTOMATICALLY
    }
    

    Do this will all resources, it’s cleaner and safer. If you are in a position to say “I need to release this resource”, you’ve done it wrong; they should be handled automatically.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Has anyone ever run into this issue before? Sometimes the string displays, sometimes half
You know how Temple Run sometimes has alerts when you open the App that
I have a user of my application that sometimes when they run the application
Sometimes I have to test some javascript code, that I use jsbin.com , but
Im using outproc session that is managed by aspnet_state. Sometimes I get run time
Sometimes I have to run multiple queries (one after another) with slightly different arguments
I have one piece of Cocoa code I wrote that takes in an XML
I have an application that sometimes will utilize a large amount of data. The
If I have a Python module implemented as a directory (i.e. package) that has
I have a jar that when run, goes through the files in a directory

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.