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.
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:
Braces appear on a line of their own (controversial!) at the same indentation level as the parent. Block content appears indented.
- class MyClass
- int Method()
- return 3;
Case labels and content are each indented.
- switch (name)
- case "John":
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.
- bool receiveMessage( Stream & stream,
- int & header, int & size );
Spaces after keywords
Spaces are placed between keywords (if, switch, for, return, etc.) and the content that follows, unless it is ;. So you have
- return (x);
.. which contrasts with no space in a function call such as ..
Spaces between mathematical operators
Brackets are not followed by spaces, but a single space appears between other mathematical operators.
- int y = (3 + 4 / 2 - 6 + (4 + 2 * array));
Spaces after commas and semicolon separators
- SomeFunc(4, x, 5);
- for(int count = 0; count != 9; count++)
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.