{"id":26,"date":"2007-10-15T13:57:21","date_gmt":"2007-10-15T12:57:21","guid":{"rendered":"http:\/\/jimblackler.com\/blog\/?p=26"},"modified":"2007-10-16T13:59:55","modified_gmt":"2007-10-16T12:59:55","slug":"standard-whitespace-visual-studio-style","status":"publish","type":"post","link":"https:\/\/jimblackler.com\/?p=26","title":{"rendered":"Standard whitespace, Visual Studio style"},"content":{"rendered":"<p>Stressing about standard code appearance in a large team project is fairly low on my list of priorities, but it is on the list.<\/p>\n<p>I&#8217;m happy to change my style to fit in, but as every project has a different &#8216;standard&#8217;, it&#8217;s annoying to have to keep changing.<\/p>\n<blockquote><p>&#8220;The wonderful thing about standards is that there are so many of them to choose from.&#8221; Grace Hopper<\/p><\/blockquote>\n<p>Visual Studio has, from VS 2002, offered a facility to &#8216;format&#8217; some code files. This format actually means convert the whitespace to a standard form. This form is apparently of Microsoft&#8217;s own design.<\/p>\n<p>I was delighted to see this feature, however, as it meant that there was an easy way that a common format for whitespace could be set in a large team. Yes there are formatting utilities out there, and VS is just one of the many IDEs available. VS is however a <em>very <\/em>popular IDE in the world of commercial development.<\/p>\n<p>The actual keypresses and modes vary by language, VS version, and configuration. In some modes it is automatic, in others you can press CTRL-E-D. You can format C++, C#, Visual Basic or XML. The .NET languages have controllable options and seem to format more effectively.<\/p>\n<p>Over the last couple of years I&#8217;ve got so familiar with the VS format that I apply the style manually when working with non-VS languages like Javascript, Actionscript and PHP.<\/p>\n<p>I have reversed engineered the default VS style so that you can do the same if you like! These are the basics. The general rule is that all tabs and spaces are stripped with the exception of:<\/p>\n<h4>Indentation<\/h4>\n<p>Braces appear on a line of their own (controversial!) at the same indentation level as the parent. Block content appears indented.<\/p>\n<div id=\"ig-sh-1\" class=\"syntax_hilite\">\n\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"csharp\" style=\"font-family:monospace\"><li><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #6666cc;font-weight: bold\">class<\/span> MyClass<\/div><\/li>\n<li><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #008000\">&#123;<\/span><\/div><\/li>\n<li><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #6666cc;font-weight: bold\">int<\/span> Method<span style=\"color: #008000\">&#040;<\/span><span style=\"color: #008000\">&#041;<\/span><\/div><\/li>\n<li><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #008000\">&#123;<\/span><\/div><\/li>\n<li><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: #0600FF;font-weight: bold\">return<\/span> <span style=\"color: #FF0000\">3<\/span><span style=\"color: #008000\">;<\/span><\/div><\/li>\n<li><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #008000\">&#125;<\/span><\/div><\/li>\n<li><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #008000\">&#125;<\/span><\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n<p>Case labels and content are each indented.<\/p>\n<div id=\"ig-sh-2\" class=\"syntax_hilite\">\n\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"csharp\" style=\"font-family:monospace\"><li><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #0600FF;font-weight: bold\">switch<\/span> <span style=\"color: #008000\">&#040;<\/span>name<span style=\"color: #008000\">&#041;<\/span><\/div><\/li>\n<li><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #008000\">&#123;<\/span><\/div><\/li>\n<li><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #0600FF;font-weight: bold\">case<\/span> <span style=\"color: #666666\">&quot;John&quot;<\/span><span style=\"color: #008000\">:<\/span><\/div><\/li>\n<li><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: #0600FF;font-weight: bold\">break<\/span><span style=\"color: #008000\">;<\/span><\/div><\/li>\n<li><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #008000\">&#125;<\/span><\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n<p>Comments appear in the first column without indentation.<\/p>\n<p>Indentation is not changed for the second and further lines for any instruction that spills over multiple lines. The default however is that it is indented from the first line.<\/p>\n<div id=\"ig-sh-3\" class=\"syntax_hilite\">\n\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"cpp\" style=\"font-family:monospace\"><li><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #008000\">&#123;<\/span><\/div><\/li>\n<li><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #0000ff\">bool<\/span> receiveMessage<span style=\"color: #008000\">&#040;<\/span> Stream <span style=\"color: #000040\">&amp;<\/span> stream,<\/div><\/li>\n<li><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: #0000ff\">int<\/span> <span style=\"color: #000040\">&amp;<\/span> header, <span style=\"color: #0000ff\">int<\/span> <span style=\"color: #000040\">&amp;<\/span> size <span style=\"color: #008000\">&#041;<\/span><span style=\"color: #008080\">;<\/span><\/div><\/li>\n<li><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #008000\">&#125;<\/span><\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n<h4>Spaces after keywords<\/h4>\n<p>Spaces are placed between keywords (if, switch, for, return, etc.) and the content that follows, unless it is ;. So you have<\/p>\n<div id=\"ig-sh-4\" class=\"syntax_hilite\">\n\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"csharp\" style=\"font-family:monospace\"><li><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #0600FF;font-weight: bold\">return<\/span> <span style=\"color: #008000\">&#040;<\/span>x<span style=\"color: #008000\">&#041;<\/span><span style=\"color: #008000\">;<\/span><\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n<p>.. which contrasts with no space in a function call such as ..<\/p>\n<div id=\"ig-sh-5\" class=\"syntax_hilite\">\n\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"csharp\" style=\"font-family:monospace\"><li><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">SomeFunc<span style=\"color: #008000\">&#040;<\/span>x<span style=\"color: #008000\">&#041;<\/span><span style=\"color: #008000\">;<\/span><\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n<h4>Spaces between mathematical operators<\/h4>\n<p>Brackets are not followed by spaces, but a single space appears between other mathematical operators.<\/p>\n<div id=\"ig-sh-6\" class=\"syntax_hilite\">\n\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"cpp\" style=\"font-family:monospace\"><li><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #0000ff\">int<\/span> y <span style=\"color: #000080\">=<\/span> <span style=\"color: #008000\">&#040;<\/span><span style=\"color: #0000dd\">3<\/span> <span style=\"color: #000040\">+<\/span> <span style=\"color: #0000dd\">4<\/span> <span style=\"color: #000040\">\/<\/span> <span style=\"color: #0000dd\">2<\/span> <span style=\"color: #000040\">-<\/span> <span style=\"color: #0000dd\">6<\/span> <span style=\"color: #000040\">+<\/span> <span style=\"color: #008000\">&#040;<\/span><span style=\"color: #0000dd\">4<\/span> <span style=\"color: #000040\">+<\/span> <span style=\"color: #0000dd\">2<\/span> <span style=\"color: #000040\">*<\/span> array<span style=\"color: #008000\">&#091;<\/span><span style=\"color: #0000dd\">4<\/span><span style=\"color: #008000\">&#093;<\/span><span style=\"color: #008000\">&#041;<\/span><span style=\"color: #008000\">&#041;<\/span><span style=\"color: #008080\">;<\/span><\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n<h4>Spaces after commas and semicolon separators<\/h4>\n<div id=\"ig-sh-7\" class=\"syntax_hilite\">\n\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"cpp\" style=\"font-family:monospace\"><li><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">SomeFunc<span style=\"color: #008000\">&#040;<\/span><span style=\"color: #0000dd\">4<\/span>, x, <span style=\"color: #0000dd\">5<\/span><span style=\"color: #008000\">&#041;<\/span><span style=\"color: #008080\">;<\/span><\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n<div id=\"ig-sh-8\" class=\"syntax_hilite\">\n\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"cpp\" style=\"font-family:monospace\"><li><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #0000ff\">for<\/span><span style=\"color: #008000\">&#040;<\/span><span style=\"color: #0000ff\">int<\/span> count <span style=\"color: #000080\">=<\/span> <span style=\"color: #0000dd\">0<\/span><span style=\"color: #008080\">;<\/span> count <span style=\"color: #000040\">!<\/span><span style=\"color: #000080\">=<\/span> <span style=\"color: #0000dd\">9<\/span><span style=\"color: #008080\">;<\/span> count<span style=\"color: #000040\">++<\/span><span style=\"color: #008000\">&#041;<\/span><\/div><\/li>\n<li><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #008000\">&#123;<\/span><\/div><\/li>\n<li><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #008000\">&#125;<\/span><\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n<p>That&#8217;s pretty much it. It&#8217;s a simple summary, no doubt a more accurate, formal one could be prepared. But that&#8217;ll give you the basics<\/p>\n<p>Other aspects of code standardisation such as standard naming conventions are not currently supported by VS. It would be interesting to see an optional scheme where violations of some convention could be flagged as errors.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Stressing about standard code appearance in a large team project is fairly low on my list of priorities, but it is on the list. I&#8217;m happy to change my style to fit in, but as every project has a different &#8216;standard&#8217;, it&#8217;s annoying to have to keep changing. &#8220;The wonderful thing about standards is that [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-26","post","type-post","status-publish","format-standard","hentry","category-general-programming"],"_links":{"self":[{"href":"https:\/\/jimblackler.com\/index.php?rest_route=\/wp\/v2\/posts\/26","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jimblackler.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jimblackler.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jimblackler.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/jimblackler.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=26"}],"version-history":[{"count":0,"href":"https:\/\/jimblackler.com\/index.php?rest_route=\/wp\/v2\/posts\/26\/revisions"}],"wp:attachment":[{"href":"https:\/\/jimblackler.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=26"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jimblackler.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=26"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jimblackler.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=26"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}