Best practice for {...} in IF with only one line

I know that these all work:

if (x > 120) digitalWrite(LEDpin, HIGH);
if (x > 120)
digitalWrite(LEDpin, HIGH);
if (x > 120){ digitalWrite(LEDpin, HIGH); }

A and B are the same except for white space, but they differ from C which uses the {...} approach.

Is there a preferred way? I lean to C just for consistency, and the habit would prevent leaving out the {...} accidentally when they should be there, as D and E below are not the same.

if (x > 120){ 
  digitalWrite(LEDpin1, HIGH);   // in the if
  digitalWrite(LEDpin2, HIGH);   // in the if
}
if (x > 120)
  digitalWrite(LEDpin1, HIGH);    //in the if
  digitalWrite(LEDpin2, HIGH);  // NOT in the if

Is there a preferred way?

Personal preferences, yes. I prefer:

if (x > 120)
{ 
  digitalWrite(LEDpin1, HIGH);
  digitalWrite(LEDpin2, HIGH); 
}

Lining up the open and close braces clearly indicates that they match, and that there IS a block of code.

Well with two lines to be included in the IF, you have to use the {...} approach or the second line will be ignored. But I do like that layout rather than this one:

if (x > 120) { 
  digitalWrite(LEDpin1, HIGH);
  digitalWrite(LEDpin2, HIGH); 
}

Or worse, this:

if (x > 120) { 
  digitalWrite(LEDpin1, HIGH);
  digitalWrite(LEDpin2, HIGH); }

But my question Paul, was really do you use the {...} when there's only one line and they're not strictly needed.

But my question Paul, was really do you use the {...} when there's only one line and they're not strictly needed.

Yes. It's become a habit. Type the statement, type the open and close curly braces, then type the body. Without even thinking, the braces are there.

If there is only one statement after the if, I don't use { } at all. There is no need for it. And unless it is very long, I prefer to put the statement on the same line.

if ( a > b )   c = 2 ;
1 Like

It's all down to personal preference, although you can easily persuade folks to argue about which is best. For another viewpoint, I don't use braces for a single statement (unless I'm posting here) and I would never put a statement on the same line as an if.

About the best you can do is pick the style you like and be consistent.

JimboZA:

if (x > 120) digitalWrite(LEDpin, HIGH);

For me, I prefer or A version or this:

JimboZA:

if (x > 120)

{ digitalWrite(LEDpin, HIGH);
}

Putting the braces with a single statement means you won't forget to add them when add another statement to the if-block later on.

Something I have done too many times in the past.

1 Like

I don't use {} for a single line IF it's on the same line as the test, it's just clearer IMO and means I can see more code at once.

	if (analogread1 >= 100 && analogread1 <= 200) pattern = 0;  
	if (analogread1 >= 300 && analogread1 <= 400) pattern = 1;  
	if (analogread1 >= 500 && analogread1 <= 600) pattern = 2;

But as soon as the code goes on another line

if (x > 120) 
      digitalWrite(LEDpin, HIGH);

I add the braces, because of the common problem of adding another line later and introducing a bug, like your example E

if (x > 120)
  digitalWrite(LEDpin1, HIGH);    //in the if
  digitalWrite(LEDpin2, HIGH);  // NOT in the if

Oops, second digitalWrite() always gets executed, regardless of the test.


Rob

1 Like

I like:

if (x > y) x = y;

... unless the conditional is long, in which case I will line-wrap without braces:

if ( (analogRead(A0) > (input_threshold * 10)) || (analogRead(A0) < (input_threshold)) )
    digitalWrite(LED, HIGH);

// But I will leave a blank line to emphasize the association above

If there's more than one statement, or during debugging, I find that I often need to add temporary statements, I'll use braces:

if (x > y) {
    // Serial.println("The value of x is %u.", x);
    x = y;
}

I like to leave a blank line after a conditional because it's easier on the eyes, but usually only if it's more than a one- or two-liner:

if (x > y) {
    
    // This will only happen when something goes horribly wrong
    digitalWrite(LED_ERROR, HIGH);
    Serial.print("Something blowed up.  Error code: %u", x);
   
    ....
}

And when I have a long conditional and multi-lines, only then do I use the brace-on-its own-line thing:

if ( (analogRead(A0) > (input_threshold * 10)) || (analogRead(A0) < (input_threshold)) )
{
    digitalWrite(LED_ERROR, HIGH);
    Serial.print("Something blowed up.  Error code: %u", x);
}

I also like to enforce my own 80-character limit. Partly because I do a lot of coding in xterm windows, but also because 100+ character lines look sloppy to me. I like my code to be clean and easy to look at.

Of course, any time breaking these rules improves readability, I don't hesitate.

I am effing tired of debugging missing brace bugs. I am effing tired of searching for then-clauses. After you have programmed long enough you too will be effing tired of those two things. Always using braces and always putting then-clauses on separate lines will slow the production of gray hair. Guaranteed.

Better hair (the indenting style is irrelevant; pick what works for you)...

if (x > 120) 
{
  digitalWrite(LEDpin, HIGH); 
}

Worse hair...

if (x > 120) digitalWrite(LEDpin, HIGH);
if (x > 120)
  digitalWrite(LEDpin, HIGH);
if (x > 120){ digitalWrite(LEDpin, HIGH); }

And when I have a long conditional and multi-lines, only then do I use the brace-on-its own-line thing:

Why have different 'rules' for different situations ?
Always use braces.
Always put them on their own line.
Always add the braces before adding the code block that they contain.
Always indent the code in the braces.

Job done

Hi, I agree with UKHeliBob, use the brackets all the time.
No matter what the layout of your sketch, if someone else is asked to peruse it for bugs then this sort of consistency will help to understand what is going on.
The brackets show the scope of the action for true or false state of the IF conditions.

Tom.... :slight_smile:

I always use braces, on their own line, and indent conditional blocks.

As per PaulS and Coding Badly 'Better Hair' examples.

i'll echo the "BetterHair" policy - if you're talking about "Best practice" as in working with others, then standard readability would be always multi-line with braces.