Standard whitespace, Visual Studio style

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’m happy to change my style to fit in, but as every project has a different ‘standard’, it’s annoying to have to keep changing.

“The wonderful thing about standards is that there are so many of them to choose from.” Grace Hopper

Visual Studio has, from VS 2002, offered a facility to ‘format’ some code files. This format actually means convert the whitespace to a standard form. This form is apparently of Microsoft’s own design.

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 very popular IDE in the world of commercial development.

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.

Over the last couple of years I’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.

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:

Indentation

Braces appear on a line of their own (controversial!) at the same indentation level as the parent. Block content appears indented.

  1. class MyClass
  2. {
  3.     int Method()
  4.     {
  5.         return 3;
  6.     }
  7. }

Case labels and content are each indented.

  1. switch (name)
  2. {
  3.     case "John":
  4.         break;
  5. }

Comments appear in the first column without indentation.

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.

  1. {
  2.     bool receiveMessage( Stream & stream,
  3.         int & header, int & size );
  4. }

Spaces after keywords

Spaces are placed between keywords (if, switch, for, return, etc.) and the content that follows, unless it is ;. So you have

  1. return (x);

.. which contrasts with no space in a function call such as ..

  1. SomeFunc(x);

Spaces between mathematical operators

Brackets are not followed by spaces, but a single space appears between other mathematical operators.

  1. int y = (3 + 4 / 2 - 6 + (4 + 2 * array[4]));

Spaces after commas and semicolon separators

  1. SomeFunc(4, x, 5);
  1. for(int count = 0; count != 9; count++)
  2. {
  3.  
  4. }

That’s pretty much it. It’s a simple summary, no doubt a more accurate, formal one could be prepared. But that’ll give you the basics

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.

Reply

Your email address will not be published. Required fields are marked *