Is it wrong to use an if statement without parentheses?

I saw the following code:

if(statement) do this; else do this; 

I do not like it, I think it is cleaner and more readable

 if(statement){ do this; }else{ do this; } 

Is it just a matter of preference, or could it be recommended in another way?

+73
coding-style if-statement
Jan 23 '10 at 22:59
source share
15 answers

The problem with the first version is that if you go back and add a second statement to the if or else clauses, remembering to add curly braces, your code will break in unexpected and funny ways.

Maintaining health, it is always wiser to use the second form.

EDIT: Ned points this out in the comments, but I think it's worth mentioning here as well. This is not just hypothetical ivory stupidity: https://www.imperialviolet.org/2014/02/22/applebug.html

+137
Jan 23
source share

One problem with leaving output blocks is else ambiguity. These C-enabled languages ​​ignore indentation and therefore are not able to separate this:

 if(one) if(two) foo(); else bar(); 

From this:

 if(one) if(two) foo(); else bar(); 
+63
Jan 23 '10 at 23:33
source share

My general model is that if it fits on a single line, I will do:

 if(true) do_something(); 

If there is an else clause or the code that I want to execute on true has a significant length, the brackets are full:

 if(true) { do_something_and_pass_arguments_to_it(argument1, argument2, argument3); } if(false) { do_something(); } else { do_something_else(); } 

Ultimately, it comes down to the subjective issue of style and readability. However, the general world of programming is divided into two sides (for languages ​​that use curly braces): either use them all the time without exception, or use them all the time with an exception. I am part of the last group.

+28
Jan 23
source share

I am using the code formatter used by the IDE. This may vary, but it can be configured in Settings / Options.

I like this:

 if (statement) { // comment to denote in words the case do this; // keep this block simple, if more than 10-15 lines needed, I add a function for it } else { do this; } 
+8
Jan 23
source share

Having brackets from the first moment should help you not debug this:

 if (statement) do this; else do this; do that; 
+6
Jan 23
source share

Use curly braces for all if statements, even simple ones. Or rewrite a simple if statement to use the ternary operator:

 if (someFlag) { someVar= 'someVal1'; } else { someVar= 'someVal2'; } 

It looks much nicer:

 someVar= someFlag ? 'someVal1' : 'someVal2'; 

But use only the ternary operator if you are absolutely sure that nothing else needs to be done in if / else blocks!

+4
Jan 23 '10 at 23:18
source share

I prefer to use curly braces. Adding braces makes reading and editing easier.

Here are some links for using curly braces:

+4
Jan 23 '10 at 23:21
source share

This is a matter of preference. I personally use both styles, if I'm sure that I will no longer need to add statements, I use the first style, but if possible, I use the second. Since you cannot add more statements to the first style, I heard that some people recommend using it. However, an additional line of code occurs in the second method, and if you (or your project) use this type of coding style, the first method is very preferable for simple if statements:

 if(statement) { do this; } else { do this; } 

However, I think the best solution to this problem is Python. With a space-based block structure, you do not have two different methods for creating the if: operator; you only have one:

 if statement: do this else: do this 

While this has a “problem” that you cannot use curly braces at all, you benefit from the fact that it no longer has lines than the first style, and it has the ability to add more statements.

+2
Jan 23 '10 at 23:05
source share

Personally, I use the first style, but prematurely discard the exception or return from the method. Similar to the Verify argument at the beginning of the function, because in these cases it is rarely necessary to deal with several cases, and never again.

Example:

 if (argument == null) throw new ArgumentNullException("argument"); if (argument < 0) return false; 

Otherwise, I use the second style.

+1
Jan 23 '10 at 23:19
source share

In my experience, the only (very) slight advantage of the first form is the readability of the code, the second form adds “noise”.

But with modern IDEs and code auto-generation (or autocompletion), I highly recommend using the second form, you won’t spend too much time typing curly braces, and you will avoid some of the most common mistakes.

There are enough energy-consuming mistakes; people simply do not open the door to the big waste of time.

One of the most important rules to keep in mind when writing code is consistency. Each line of code should be written the same, regardless of who wrote it. Strict error prevention from "occurring";)

This is the same as explicit and explicit designation of your variables, methods, files or their correct indentation ...

When my students accept this fact, they stop struggling with their own source code and begin to realize that coding is really interesting, stimulating, and creative. They challenge their mind, not their nerves!

+1
Jan 24 '10 at 0:09
source share

The following "rule" is as follows:

If the if statement is testing to do something (IE call functions, setting variables, etc.), use curly braces.

 if($test) { doSomething(); } 

This is because I feel that you need to make it clear which functions are called and where the program flow goes, under what conditions. If the programmer understands exactly what functions are called and which variables are set in this condition, it is important to help them understand exactly what your program is doing.

If the if statement is being tested to stop doing something (control IE flow in a loop or function), use one line.

 if($test) continue; if($test) break; if($test) return; 

In this case, which is important for the programmer, quickly discovers that in exceptional cases you do not want the code to run, and all this is covered in $ test, and not in the execution unit.

+1
Feb 10 '14 at 8:08
source share

I always tried to make my code standard and look as close to it as possible. This makes it easier for other users to read when they are responsible for updating it. If you make your first example and add a line in the middle to it, this will fail.

Does not work:

if (expression) do it; and this; still do it;

0
Jan 23
source share

I agree with most of the answers that it is better to be explicit in your code and use curly braces. Personally, I would accept a set of coding standards and ensure that everyone on the team knows them and is consistent. Where I work, we use the coding standards published by IDesign.net for .NET projects.

0
Jan 23 '10 at 23:13
source share

My personal preference is to use space and parenthesis mixing:

 if( statement ) { // let do this } else { // well that sucks } 

I think it looks clean and makes my code very easy to read and, most importantly, debugging.

0
Jan 24 '10 at 2:51
source share

I prefer to insert braces. But sometimes the trojan operator helps.

Instead:

 int x = 0; if (condition) { x = 30; } else { x = 10; } 

You just need to do: int x = condition ? 30 : 20; int x = condition ? 30 : 20;

Also imagine the case:

 if (condition) x = 30; else if (condition1) x = 10; else if (condition2) x = 20; 

It would be much better if you enclosed curly braces.

0
Jan 29 '10 at 8:00
source share



All Articles