Syntax Question regarding {}- This one is an easy one...I hope

Why does every example code I find, even in the wiki on arduino.cc write code like this

if (condition1) {
  // do Thing A
}
else if (condition2) {
  // do Thing B
}
else {
  // do Thing C
}

instead of :

if (condition1)
 {  
  // do Thing A 
 }
else if (condition2) 
 {
  // do Thing B
 }
else
 {
  // do Thing C
 }

I'm having a really hard time keeping track of them, so I'm looking for a solution but I want to make sure my code is easily readable by other users

It's just a matter of style. Just choose one that suits you and stick with it. The compiler doesn't care.

Most C/C++ style guides I've seen use the first version. But there are plenty of examples out there that use the second style which is the one I prefer. But it is just a style, it makes no practical difference.

Steve

One thing that i think nearly all agree on is that

}
 } else {
  // do Thing C
}

or

}
else {
  // do Thing C
} }

or even

}
else {
  // do Thing C 
  thingC(); }

is a style that is not encouraged, that closing brace should be on a line by it self.

Just a matter of preference. I prefer the first one because it gives me more lines in the window. I do agree that the terminating curly should be on a line of its own though, as it's usefulness in visualizing the block trumps the vertical space sving.

I prefer to have the curlys in a vertical line for structure and visibility. I also like to comment closing curlys with a quick reference to what they're closing. For example

void setup() 
{
    

}//setup

void loop() 
{
    if( expression )
    {
        do something
    }//if
    else
    {
        do something else
    }//else

    switch( foo )
    {
        case    1:
            do stuff
        break;

        case    2:
            do other stuff
        break;
        
    }//switch

}//loop

It takes more space but produces more readable code that, I think, is likely to be right the first time.

I prefer curly brackets on their own line and have Auto Format configured to do it for me

Here is my formatter configuration file:

# This configuration file (formatter.conf) contains a selection of the available options 
# provided by the formatting tool "Artistic Style"
#
# http://astyle.sourceforge.net/astyle.html
#
# If you wish to change them, don't edit this file.
# Instead, copy it in the same folder of file "preferences.txt" and modify 
# the copy. This way, you won't lose your custom formatter settings when 
# upgrading the IDE
# If you don't know where file preferences.txt is stored, open the IDE, 
# File -> Preferences and you'll find a link


mode=c


# 2 spaces indentation
indent=spaces=2


# Put braces on separate lines
style=break


# also indent macros
indent-preprocessor


# indent classes, switches (and cases), comments starting at column 1
indent-classes
indent-switches
indent-cases
indent-col1-comments


# put a space around operators
pad-oper


# put a space after if/for/while
pad-header


# if you like one-liners, keep them
keep-one-line-statements


remove-comment-prefix

johnwasser:
Here is my formatter configuration file:

Hi,
johnwasser, how do we use this configuration file?

Regards.

johnwasser, how do we use this configuration file?

Did you read the instructions at the top of the file that John posted ?

Here is my formatter configuration file

really cool, but somehow not all options as described in the html file are available. so

style=attach
break-closing-braces

is what i would like.

UKHeliBob:
Did you read the instructions at the top of the file that John posted ?

I beg your pardon,
yes I did go through the instructions, looked up the website too.
Could'nt get a handle on the formatter.conf file.

Regards.

Could'nt get a handle on the formatter.conf file.

I am not sure what you mean by that

Look in the folder suggested in the comments at the top of the file posted by John

# If you don't know where file preferences.txt is stored, open the IDE, 
# File -> Preferences and you'll find a link

If there is already a file in the folder named formatter.conf then you can edit it with a text editor. If it does not exist then create one but in either case use a text editor such as Notepad or Notepad++ not a Word processor and if you are using Windows and have the "hide extensions to make things more complicated" option turned on then make sure that the file does not end up being named formatter.conf.txt or some other unwanted name.

As a start I suggest that you copy the text of John's file into yours but keep a copy of the original if it exists so that you can revert to it if necessary

Here is what I have in mine

# This configuration file contains a selection of the available options provided by the formatting tool "Artistic Style"
# http://astyle.sourceforge.net/astyle.html
#
# If you wish to change them, don't edit this file.
# Instead, copy it in the same folder of file "preferences.txt" and modify the copy. This way, you won't lose your custom formatter settings when upgrading the IDE
# If you don't know where file preferences.txt is stored, open the IDE, File -> Preferences and you'll find a link

# 2 spaces indentation
indent=spaces=2

# also indent macros
indent-preprocessor

# indent classes, switches (and cases), comments starting at column 1
indent-classes
indent-switches
indent-cases
indent-col1-comments

# put a space around operators
 pad-oper

# put a space after if/for/while
 pad-header

# Move opening brackets onto new line
 --style=allman --style=bsd --style=break -A1

# delete empty lines in functions
 --delete-empty-lines 

# Insert space padding around operators. 
 --pad-oper

Thanks UKHeliBob, with your instructions I created a text file "formatter.conf", in my Arduino15 folder in which the preferences.text is found.
Thanks to johnwasser for sharing this method.
BTW is there a way to make the text bold in the Arduino IDE it will be a relief for the ageing eyes.

BTW is there a way to make the text bold in the Arduino IDE it will be a relief for the ageing eyes.

You can change the 'editor font size' that should help.

Deva_Rishi:
You can change the 'editor font size' that should help.

Yes I did that thanks for the tip