1

This might be a silly question, but I'm new to C++ and programming in general and I couldn't find the answer on here. I know in C++, the { } are optional in some cases. For example, if you have a simple if statement where only one operation is performed, you don't need to surround it with { }.

I was just wondering if the extra brackets have any effect (even the smallest) on the speed of the program. The reason I ask is because I always include the curly brackets in all of my statements even if not required, just because I like to block out my code.

My personal preference is:

   if (foo)
   {
        bar;
   }

Instead of simply

   if (foo)
       bar;

I just like the way it looks when reading the code. But, if this actually has an effect on the speed of the code, it's probably not a good idea. Does anyone know if extra brackets affects the speed? Thanks.

Sabien
  • 219
  • 2
  • 16
  • Check this post out for a good answer... http://stackoverflow.com/questions/3568503/extra-brace-brackets-in-c-code –  Apr 18 '14 at 00:04
  • This question shows a severe lack of understanding of what c++ is. – shoosh Apr 18 '14 at 00:08
  • @shoosh Could you elaborate on that please? As I said I'm new to this. So yes, obviously I lack some understanding of "what c++ is." – Sabien Apr 18 '14 at 00:10
  • @Sabien This question indicates that you think that the actual textual code you write is being read during the run time of the program. While this is the case for (some) interpreted languages, it can't be further from reality for c++ which is a compiled language. If you realize c++ is compiled, I don't see how you could think such a syntactical difference which doesn't change the meaning of the program in any way can have any effect at run time. – shoosh Apr 18 '14 at 00:20
  • @shoosh Thank you for sharing. We briefly talked about what happens when source code is compiled but details were left out. Basically we leaned what the preprocessor does, then the compiler (which as I understood it turns the source code into 1's and 0's and stores that in the object file). In my mind, I was thinking all of the source code, excluding comments, was turned into 1's and 0's, including punctuation...hence why I was thinking adding extra brackets might slow it down because it would add more 1's and 0's. But I understand now, thank you. – Sabien Apr 18 '14 at 00:29
  • 2
    @Sabien everything on a computer is stored as 1's and 0's. it's the meaning of those bits that matter. When you just write your code and save it as a .cpp file using your editor, the meaning of the bits is the text you wrote. When you compile the code, the compiler takes this text, translates it into assembly code and writes the binary encoding of the assembly into an .exe file. Here's more: http://en.wikipedia.org/wiki/Compiler – shoosh Apr 18 '14 at 00:37

3 Answers3

7

No it does not.
In general, due to the "as if"-rule, the compiler has a lot of leeway to optimize things.

Still, it is a style issue, and there are seldom straight answers everyone agrees on.

There are people who only use braces of any kind if they either significantly clarify the code or are neccessary, and those who always use a code block for conditionals and loops.
If you work in a team/on contract/on an inherited codebase, try to conform to their style, even if it's not yours.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
  • 2
    most people I know agree that always using brackets is better though – Jules G.M. Apr 18 '14 at 00:04
  • 1
    @Julius It's a style issue. There are plenty of excellent coders that do and plenty that don't. – Jeff Apr 18 '14 at 00:06
  • 1
    re "No it does not", that can in principle depend on the execution environment. There are some [C++ intepreters](http://stackoverflow.com/questions/69539/have-you-used-any-of-the-c-interpreters-not-compilers). I don't know if any of them interpret raw source code text, but it's not that farfetched to think that the existence of interpreters implies the existence of at least one really awfully inefficient one... ;-) – Cheers and hth. - Alf Apr 18 '14 at 03:27
0

It has the same result for the compiler. It's like initialize a variable like these:

int a = 0;
int a {0};
int a (0);

The have the same result as well. It's a matter of style.

  • To be fair, if you change the type of `a`, those statements can differ greatly. – chris Apr 18 '14 at 00:17
  • what would be the change –  Apr 18 '14 at 00:19
  • 2
    Let's say `a` is a `std::vector`. The first won't compile due to the appropriate conversion constructor being explicit. The second initializes it to have one 0. The third initializes it to a size of 0 (empty). – chris Apr 18 '14 at 05:31
0

The curly braces are there to help the compiler figure out the scope of a variable, condition, function declaration, etc. It doesn't affect the runtime performance once the code is compiled into an executable. Braces make the code more maintainable

It helps to debug the code with less pain, imagine below code snippet and you need to evaluate the do_some_operation by putting breakpoint. The second option will serve the purpose better

if( some_condition ) { do_some_operation; }
--------------------------
if( some_condition )
{
    do_some_operation;
}
DNamto
  • 1,342
  • 1
  • 21
  • 42