Geeklog
There's no place like 127.0.0.1 - ramblings about programming

Coding Style (The Law)

September 3, 2009 18:29 by The Geek

There are loads of arguments on the web about how you should style your code.  These are my preferences, you can think of them as the law Wink.

  1. Use StyleCop.  This is an absolute no-brainer.  If you live in the Microsoft world, then you should use this.  Yes, it is only for C# so if you write in C++ I don't think it will help.  If you write in VB.NET, then unlucky - hopefully your employer forces you to do so and it is not by choice Tongue out.  In case you are too lazy to click on the link, StyleCop analyses your source code to ensure that it doesn't break any styling conventions.
  2. Use FxCop.  This one can be really important.  FxCop will check your compiled .NET assembly to check for various things ranging from overflows to attribute usage.
  3. Use Sandcastle (and if you want, its GUI).  It's a great tool that will create documentation for your assemblies.  Well worth it if you work in a team or if you expose your APIs to third parties.
  4. Use Guidelines and don't type past them!  I like code that is 80 columns wide - that means that I don't need to scroll horizontally on my laptop (1280*800) and I can view 2 files side by side on my main workstation (1920 * 1200).

If you are working as part of a team (and sometimes even if you are not), you should be using a continuous integration server.  CruiseControl.NET seems to be held in high regard although I have recently had a good experience with TeamCity.  It is very easy to set up - the only downside is that, unless you pay, it is limited to 20 builds.  While this seems like plenty, if you have a "build" for the actual compilation, another for your unit tests, and one for each of the tasks above, you soon need to spend some £££.

Regarding StyleCop, I use all of the default rules except for "SA1027: TabsMustNotBeUsed".  IMO, it is just silly to use spaces instead of tabs.  Why should I use 4 key presses when one will do?  Tabs also mean that if a member of your team is a little bit odd and they like 2 spaces - or 8 - then they can just configure Visual Studio so that it looks right for them.  The only problem would be if a team member actual likes pressing space instead of tab (thanks to JK for pointing this out).

Regarding FxCop, suppressions are the last resort! Try very hard not to use them.

I will post soon with a project that obeys the rules.

UPDATE:  You can download an example of The Law at the end of this post.


Comments