{"id":11298,"date":"2016-03-03T06:00:00","date_gmt":"2016-03-03T14:00:00","guid":{"rendered":"https:\/\/www.sapien.com\/blog\/?p=11298"},"modified":"2016-03-01T08:09:24","modified_gmt":"2016-03-01T16:09:24","slug":"theres-only-one-right-place-for-curly-braces","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2016\/03\/03\/theres-only-one-right-place-for-curly-braces\/","title":{"rendered":"There&#8217;s only one right place for curly braces"},"content":{"rendered":"<p>If you ever want to start an argument with programmers or scripters, mention your preferred code or script formatting habit.<\/p>\n<p>A tweet about the <a href=\"https:\/\/twitter.com\/search?q=powershell%20backtick&amp;src=typd\">PowerShell backtick continuation character<\/a> or the placement of comment-based help for a function will elicit enough reaction to bring down a small server.<\/p>\n<p>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?<\/p>\n<p style=\"padding-left: 30px;\"><a href=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2016\/02\/clip_image002-4.jpg\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border: 0px;\" title=\"clip_image002\" src=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2016\/02\/clip_image002_thumb-4.jpg\" alt=\"clip_image002\" width=\"472\" height=\"312\" border=\"0\" \/><\/a><br \/>\n<a href=\"http:\/\/s3.10pm.com\/0915\/89321_programmers-will-always-know\/\">From 10PM.com<\/a><\/p>\n<p>First, PowerShell MVP <a href=\"https:\/\/twitter.com\/alexandair\">Aleksandar Nikolic<\/a> will remind you (and rightly so!) that &#8220;curly braces&#8221; is redundant, because all braces are curly. That&#8217;s how you distinguish braces from brackets.<\/p>\n<p>Then, everyone who&#8217;s not on deadline, and a few people who are, will weigh in about how their curly brace placement is the right way. It&#8217;s the professional way. It&#8217;s the clearest way. It&#8217;s the historic way. It&#8217;s the new way.<\/p>\n<p>In general, PowerShell doesn&#8217;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.<\/p>\n<h1>Place open brace on new line (or not!)<\/h1>\n<p>PowerShell Studio wouldn&#8217;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.<\/p>\n<p>To declare your choice:<\/p>\n<ol>\n<li>In PowerShell Studio, click <b>Home<\/b> and in the Windows group, click <b>Options<\/b>.<\/li>\n<li>Under Editor, click <b>Formatting<\/b>.<\/li>\n<li>Check or uncheck: <b>Place open brace on a new line<\/b>.<\/li>\n<\/ol>\n<p>The script diagram shows you the effect.<\/p>\n<p style=\"padding-left: 30px;\"><a href=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2016\/02\/clip_image004-3.jpg\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border: 0px;\" title=\"clip_image004\" src=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2016\/02\/clip_image004_thumb-3.jpg\" alt=\"clip_image004\" width=\"588\" height=\"462\" border=\"0\" \/><\/a><\/p>\n<p>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.<\/p>\n<p style=\"padding-left: 30px;\"><a href=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2016\/02\/clip_image005.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border: 0px;\" title=\"clip_image005\" src=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2016\/02\/clip_image005_thumb.png\" alt=\"clip_image005\" width=\"594\" height=\"146\" border=\"0\" \/><\/a><\/p>\n<p>As a result, when you hit a trigger or click the amazing <b>Format Script<\/b> button (Home\/Edit), PowerShell Studio formats your script with the curly braces where you want them.<\/p>\n<p>Remember that <strong>Place open brace on a new line<\/strong> affects only basic script blocks, like those in an IF statement. It doesn&#8217;t affect other instances of curly braces, like those in hash tables, functions, or even the script blocks in event handlers.<\/p>\n<p>So, there&#8217;s only one right place for curly braces in a script block. It&#8217;s where you want them.<\/p>\n<p>&nbsp;<\/p>\n<p><em>June Blender is a technology evangelist at SAPIEN Technologies, Inc. and a Windows PowerShell MVP. You can reach her at <\/em><a href=\"mailto:juneb@sapien.com\"><em>juneb@sapien.com<\/em><\/a><em>\u00a0and follow her on Twitter at <\/em><a href=\"https:\/\/twitter.com\/juneb_get_help\"><em>@juneb_get_help<\/em><\/a><em>.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":31,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[1034,703],"tags":[1064,85,934,1062,1016,1063],"class_list":["post-11298","post","type-post","status-publish","format-standard","hentry","category-best-practices","category-powershell-studio","tag-curly-braces","tag-formatting","tag-juneb","tag-options","tag-powershell-studio","tag-preferences"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/11298","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/31"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=11298"}],"version-history":[{"count":7,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/11298\/revisions"}],"predecessor-version":[{"id":11305,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/11298\/revisions\/11305"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=11298"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=11298"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=11298"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}