If you ever want to start an argument with programmers or scripters, mention your preferred code or script formatting habit.
A tweet about the PowerShell backtick continuation character or the placement of comment-based help for a function will elicit enough reaction to bring down a small server.
But nothing gets people more riled up than the position of curly braces in a script block. On the same line or a separate line?
First, PowerShell MVP Aleksandar Nikolic will remind you (and rightly so!) that “curly braces” is redundant, because all braces are curly. That’s how you distinguish braces from brackets.
Then, everyone who’s not on deadline, and a few people who are, will weigh in about how their curly brace placement is the right way. It’s the professional way. It’s the clearest way. It’s the historic way. It’s the new way.
In general, PowerShell doesn’t care about white space or line breaks before curly braces in script blocks. They matter only in structures like hash tables. But, everyone else cares.
Place open brace on new line (or not!)
PowerShell Studio wouldn’t dare to make the critical choice of curly brace placement for you. Instead, you decide where you want your curly braces in script blocks.
To declare your choice:
- In PowerShell Studio, click Home and in the Windows group, click Options.
- Under Editor, click Formatting.
- Check or uncheck: Place open brace on a new line.
The script diagram shows you the effect.
On the same Formatting page, you can also set your Triggers, that is, the characters on which PowerShell Studio evaluates and reformats for you, if necessary.
As a result, when you hit a trigger or click the amazing Format Script button (Home/Edit), PowerShell Studio formats your script with the curly braces where you want them.
Remember that Place open brace on a new line affects only basic script blocks, like those in an IF statement. It doesn’t affect other instances of curly braces, like those in hash tables, functions, or even the script blocks in event handlers.
So, there’s only one right place for curly braces in a script block. It’s where you want them.
June Blender is a technology evangelist at SAPIEN Technologies, Inc. and a Windows PowerShell MVP. You can reach her at juneb@sapien.com and follow her on Twitter at @juneb_get_help.
RT @SAPIENTech: There’s only one right place for curly braces: If you ever want to start an argument with programmers or script… https://…
Want to start an argument w/o programmer/scripter? Tell them where to put (curly) braces. @SAPIENTech @alexandair https://t.co/EHbiRuonra
@SAPIENTech @alexandair So my question is, where do you put the braces @juneb_get_help?
There’s only one right place for curly braces https://t.co/pZR9ZUk6er
There’s only one right place for curly braces https://t.co/JCyuwMIirk
RT @SAPIENTech: There’s only one right place for curly braces: If you ever want to start an argument with programmers or script… https://…
@Jaap_Brasser @SAPIENTech @alexandair Tweet your format options! Here are mine. #PowerShellStudio https://t.co/EUBO4gBslo
@juneb_get_help @Jaap_Brasser @SAPIENTech @alexandair I don’t like all the brace-only lines! Gah! Heresy!
@Jaap_Brasser @SAPIENTech @alexandair Tweet your formatting options, @barbariankb. #PowerShellStudio #OneRightWay
@juneb_get_help @Jaap_Brasser @SAPIENTech @alexandair Here’s a quick example of the #OneRightWay 😉 https://t.co/H8jfzcGr0f
@SAPIENTech #OneRightWay #Powershell Where do you put curly braces?
https://t.co/ktYBxalswe
https://t.co/EHbiRuonra
I’m a same-liner, for two reasons. Firstly, putting the opening brace on a separate line feels like cutting the preceding statement in half. It’s like stopping mid-sentence, and feels and looks disjointed. Secondly, it wastes so much space that I can see far less code on screen at a time which makes coding and debugging more of a faff; especially if you have some If-Else constructs. (I’d have the “} else {” all on one line for example rather than on three.)
I’d be interested to hear what the arguments are for the separate lines format though.
@barbariankb @juneb_get_help @Jaap_Brasser @SAPIENTech @alexandair Screens are usually wider than they are tall. Vertical space is precious.
In powershell it’s not just personal style like it is in other languages. For if you don’t open a script lock on the same line it’s going to mean some thing so different either behaving different or not workin at all
1..10 | foreach
{
$_ * $_
}
Is very different than if you had the opening brace on the same line.
@barbariankb @juneb_get_help @Jaap_Brasser @SAPIENTech @alexandair I prefer Stroustrup style without a cuddled else: https://t.co/LGLwet0R64
@mikefrobbins @barbariankb @Jaap_Brasser @SAPIENTech @alexandair You should add that to your resume. 🙂 I just want to say “cuddled else.”
@devblackops @barbariankb @juneb_get_help @Jaap_Brasser @SAPIENTech @alexandair 1stguy – vertical space is precious! https://t.co/UYiDfQJHZZ
@devblackops @barbariankb @juneb_get_help @Jaap_Brasser @SAPIENTech @alexandair 2ndguy – No, horizontal space! https://t.co/hv6ZPT1p6Y
@mikefrobbins @juneb_get_help @Jaap_Brasser @SAPIENTech @alexandair I used to prefer w/o cuddling but as time goes on I appreciate it more.
@mikefrobbins @barbariankb @juneb_get_help @Jaap_Brasser @SAPIENTech @alexandair isn’t that Allman?
@juneb_get_help @SAPIENTech @alexandair How about no braces and just use indents 🙂 (ducks)
@barbariankb @juneb_get_help @Jaap_Brasser @SAPIENTech @alexandair new lines for braces are my jam!
@mcnabbmh @juneb_get_help @Jaap_Brasser @SAPIENTech @alexandair Why?
I have said it before and I will say it again. Powershell studio is the best IDE I have used in my career of 20+ years as a developer. Please keep up the great work!