Should brackets be used for a single line of conditional statements?

Besides readability, are there any differences in performance or compilation time when a single-line loop / conditional statement is written with and without scraps?

For example, are there differences between the following:

if (a > 10) a = 0; 

and

 if (a > 10) { a = 0; } 

?

+4
source share
6 answers

No, there is no difference, the compiler will delete the insignificant curly braces, line breaks, etc.

The compilation time will vary slightly, but so insignificantly that you have already lost much more time reading this answer than you will return to compilation speed. As computing power increases, this cost decreases even more, but there is no cost to reduce readability.

In short, do what is readable; it has no meaning in any other sense.

+2
source

Contrary to several answers, there is a finite, but negligible performance difference at compile time. There is zero difference at runtime.

+4
source

Of course, there is no difference in performance. But there is a difference in the possibility of introducing errors:

 if (a>10) a=0; 

If someone extends the code and writes later,

 if (a>10) a=0; printf ("a was reset\n"); 

This will always be printed due to missing braces. Some people ask that you always use curly braces to avoid such errors.

+4
source

Machine code does not contain such braces. After compilation, there are no more {} . Use the most readable form.

+2
source

Well, of course, there is no difference between them as such at runtime. But you should definitely use the 2nd method to save your code.

Why am I saying this, suppose in the future you need to add a few more lines to the if-else block to expand them. Then, if you have the first method included in your old code, you need to add braces before adding new code. What you do not need to do in the second case.

Thus, it is much easier to add code to the second path in the future than to the first.

In addition, if you are using the first method, you should make input errors such as semi-colon after your if , for example:

 if (a > 0); System.out.println("Hello"); 

So you can see that your Hello will always print. And you can easily remove these errors if you have curly braces attached to your if .

+1
source

It depends on the rest of the encoding rules. I don't see the problem resets braces if the opening bracket is always on the line by itself. If the opening bracket is at the end of the if line however, I am too easily overlooked when adding content. So I'll go too:

 if ( a > 10 ) { a = 0; } 

regardless of the number of rows, or:

 if ( a > 10 ) { // several statements... } 

with:

 if ( a > 10 ) a = 0; 

when there is only one statement. The important thing, however, is that all code must be consistent. If you are working on an existing code base that uses several different styles, I would always use curly braces in the new code, since you cannot rely on the code style to make sure that if they were there, they would be in a very visible place .

-1
source

Source: https://habr.com/ru/post/1444776/


All Articles