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.
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.
# 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
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.