Jan Schreuder on .Net

.Net code samples, experiences, observations

View my professional profile on LinkedIn

Recent Posts

Tags

News

  • Inappropriate comments will be deleted at my discretion.

    The information and code samples in this weblog is provided "AS IS" without warranty of any kind, either expressed or implied, including but not limited to the merchantability and/or fitness for a particular purpose.

Community

Email Notifications

Tool suppliers

Tools

General

Microsoft

Favorite blogs

Archives

Coding standards, Coding rules, Coding guidelines, etc...

If there's one thing that developers usually just cannot (or will not) agree to, then it's Coding standards/rules/guidelines or whatever name you want to give it. I posted a short item about a new tool from Microsoft that checks if your code matches the Microsoft guidelines and sure enough, I'm already getting mails on the subject explaining that Microsoft's rules suck and that they use one that's better. I thought it might just be a great subject for a discussion. But let me share my views on the subject first.

Coding standards, my view

I've been working as a developer in various roles and projects now for over 20 years. Yes, I'm that old, but it's to show that I speak from experience on this subject. Each project I worked on has had one or another form of coding guidelines or standards. In one project, they even called it a coding convention in an attempt to make it more important. My reaction in most occasions was: "What ever..."

The thing with this subject is, that developers simply cannot agree to one single guideline. Every developer thinks he/she is the best in his/her area and therefor the expert to tell others how to write their code. Well, they're wrong.

A coding standard is as good as the method to enforce it. If it's not enforced, then it's useless. And in 99% of all projects you will see that coding standards are not enforced. In my view, the only standard that works is the standard that you can enforce using tools. The guidelines checked by Microsoft's FxCop are a good example. It always uses the same rules for all code and always warns in the same way. There are other tools that do that as well, like DevPartner from Compuware. You can agree to those rules or not, but at least it leaves little room for discussion and I see FxCop being more and more accepted.

And now there's StyleCop. It checks your code against the coding rules as used at Microsoft development centers. Again, you can discuss if these rules are correct, but you get a tool that validates the code and tells you were you have strayed from the narrow path. My feeling is that although I don't entire agree to all rules, at least I can force my team to stick to these rules. Simply by breaking the build if they don't comply to the coding rules.

There are tools that allow you to write your own custom rules, like ReSharper and DevPartner. Useful as this can be, what do you do with code generated by Microsoft code generators. Think only of generated code for DataSet objects and WinForms. It's highly unlikely that this code will adhere to your guidelines. But will the use the Microsoft guidelines? It's a better bet then with your custom rules.

Another misconception is that these rules improve the quality. But to be frank, this is mostly bullshit. The rules checked by FxCop (and similar tools) check for code quality with respect to the use of the .Net framework. StyleCop (and similar tools) check for style issues. But that does not improve code quality. That can only be achieved by testing your work and using defense programming techniques. But I digress.

So whatever suits you, I guess. I'll stick to using the Microsoft Guidelines and use their tools to check if I've done it right. But whatever coding standard you choose, make sure your entire team sticks to those rules, otherwise they're completely useless. Unless it's to satisfy management that you thought about code quality.

But please, feel free to comment. I will approve them as soon as possible, but with the weekend starting in the Netherlands it might take some time. But I'd love to hear how the community feels about coding standards, the new Microsoft tool, and other things regarding this subject.

Comments

Erwyn van der Meer said:

StyleCop is not a Microsoft product. It was developed by a couple of Microsoft employees for their own product development. They decided to release it in public, because it might be of use to other developers. And it sure is useful! However, the rules in this tool should not be viewed as THE Microsoft coding guidelines rules. Feel free to turn off rules you don't like. For example, I always turn off the strange rule that says you should put your using statements for namespaces within a namespace declaration.

For more background info about StyleCop, see blogs.msdn.com/.../clearing-up-confusion.aspx

# July 26, 2008 7:03 AM

Nathan Pledger said:

I use and have encouraged our team at work to use the Philips Coding Standards (www.tiobe.com/.../gemrcsharpcs.pdf) with a few defined exceptions:

- private members to be prefixed _ and be in camel case

- constants are in CAPS

I'm a big fan of not relying on syntax highlighting or intellisense to help guide the eye of the purpose of a line of code, so will utilise every case/punctuation method I can to help differentiate items.

And you're right, a standard is not a standard if people don't use it. While (I believe) my coding standards are exceptional (I must confess to a small amount of OCD in my styling of code!), others often don't see it as being quite so important. I think we have a happy medium, though. Any shared projects do tend to meet these standards.

# July 26, 2008 4:30 PM

Jan Schreuder said:

Good to know Erwyn, thanks. I was under the impression that, although it was an internal tool that it would get more a Microsoft badge. Kinda like Sandcastle.

# July 28, 2008 9:07 AM

Jan Schreuder said:

Thanks Nathan. Interesting read, those Philips Coding Standards. I noticed also that a lot of the rules are found in FxCop. A number of them are also checked by StyleCop. Using those tools might help you enforce the coding standards.

# July 28, 2008 3:52 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)