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
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.
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.
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.
... 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:
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)...
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.
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.
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.