<p>I'm a recent AI graduate (circa 2 years) working for a modest operation. It has fallen to me (primarily as I'm the first 'adopter' in the department) to create a basic (read useful?) C# coding standards document. </p>

<p>I think I should explain that I'm probably the most junior software engineer going, but I'm looking forward to this task as hopefully I might actually be able to produce something half usable. I've done a pretty extensive search of the Internet and read articles on what a coding standards document should / should not contain. This seems like a good as place as any to ask for some suggestions.</p>

<p>I realise that I am potentially opening a door to a whole world of disagreement about 'the best way to do things'. I both understand and respect the undeniable fact that each programmer has a preferred method of solving each individual task, as a result I'm not looking to write anything so draconianly proscriptive as to stifle personal flair but to try and get a general methodology and agreed standards (e.g. naming conventions) to help make individuals code more readable.</p>

<p>So here goes .... any suggestions? Any at all?</p>

[{'answer_id': 14973, 'author': 'Keith', 'author_id': 905, 'author_profile': 'https://Stackoverflow.com/users/905', 'pm_score': 2, 'selected': False, 'text': "<p>Microsoft's own rules are an excellent starting point.  You can enforce them with FxCop.</p>\n"}, {'answer_id': 14974, 'author': 'ESV', 'author_id': 150, 'author_profile': 'https://Stackoverflow.com/users/150', 'pm_score': 8, 'selected': True, 'text': '<p>We start with </p>\n\n<ul>\n<li>Microsoft\'s .NET guidelines: <a href="http://msdn.microsoft.com/en-us/library/ms229042.aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/ms229042.aspx</a> (link updated for .NET 4.5)</li>\n<li>Microsoft\'s C# guidelines: <a href="http://blogs.msdn.com/brada/articles/361363.aspx" rel="noreferrer">http://blogs.msdn.com/brada/articles/361363.aspx</a>.</li>\n</ul>\n\n<p>and then document the differences from and additions to that baseline.</p>\n'}, {'answer_id': 14981, 'author': 'Dale Ragan', 'author_id': 1117, 'author_profile': 'https://Stackoverflow.com/users/1117', 'pm_score': 4, 'selected': False, 'text': '<p>I have always used Juval Lowy\'s <a href="http://www.idesign.net/idesign/download/IDesign%20CSharp%20Coding%20Standard.zip" rel="nofollow noreferrer">pdf</a> as a reference when doing coding standards / best practices internally.  It follows very close to <a href="http://msdn.microsoft.com/en-us/library/bb429476(VS.80).aspx" rel="nofollow noreferrer">FxCop</a>/<a href="http://blogs.msdn.com/sourceanalysis/archive/2008/05/23/announcing-the-release-of-microsoft-source-analysis.aspx" rel="nofollow noreferrer">Source Analysis</a>, which is another invaluable tool to make sure that the standard is being followed.  Between these tools and references, you should be able to come up with a nice standard that all your developers won\'t mind following and be able to enforce them.</p>\n'}, {'answer_id': 14982, 'author': 'denis phillips', 'author_id': 748, 'author_profile': 'https://Stackoverflow.com/users/748', 'pm_score': 5, 'selected': False, 'text': '<p><a href="http://www.idesign.net" rel="noreferrer">IDesign</a> has a C# coding standards document that is commonly used. Also see the <a href="https://rads.stackoverflow.com/amzn/click/com/0321545613" rel="noreferrer" rel="nofollow noreferrer">Framework Design Guidelines 2nd Ed</a>.</p>\n'}, {'answer_id': 14985, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 3, 'selected': False, 'text': '<p>The other posters have pointed you at the baseline, all I would add is make your document short, sweet, and to the point, employing a heavy dose of Strunk and White to distinguish the "must haves" from the "it would be nice ifs".</p>\n\n<p>The problem with coding standards documents is that nobody really reads them like they should, and when they do read them, they don\'t follow them. <strong>The likelihood of such a document being read and followed varies inversely with its length.</strong></p>\n\n<p>I agree FxCop is a good tool but too much of this can take all the fun right out of programming, so be careful.</p>\n'}, {'answer_id': 14989, 'author': 'Andrew Grant', 'author_id': 1043, 'author_profile': 'https://Stackoverflow.com/users/1043', 'pm_score': 5, 'selected': False, 'text': "<p>Ironically setting the actual standards are likely to be the easy part.</p>\n\n<p>My first suggestion would be to elicit suggestions from the other engineers about what they feel should be covered, and what guidelines they feel are important. Enforcing any kind of guidelines requires a degree of buy-in from people. If you suddenly drop a document on them that specifies how to write code you'll encounter resistance, whether you're the most junior or senior guy.</p>\n\n<p>After you have a set of proposals then send them out to the team for feedback and review. Again, get people to all buy into them.</p>\n\n<p>There may already be informal coding practices that are adopted (e.g prefixing member variables, camelcase function names). If this exists, and most code conforms to it, then it will pay to formalize its use. Adopting a contrary standard is going to cause more grief than it's worth, even if it's something generally recommended.</p>\n\n<p>It's also worth considering refactoring existing code to meet the new coding-standards. This can seem like a waste of time, but having code that does not meet the standards can be counter-productive as you will have a mish-mash of different styles. It also leaves people in a dilemma whether code in a certain module should conform to the new standard or follow the existing code style. </p>\n"}, {'answer_id': 15020, 'author': 'Rob Cooper', 'author_id': 832, 'author_profile': 'https://Stackoverflow.com/users/832', 'pm_score': 0, 'selected': False, 'text': '<p>I think I echo the other comments here that the MS guidlines already linked are an excellent starting point. I model my code largely on those.</p>\n\n<p>Which is interesting because my manager has told me in the past that he is not too keen on them :D</p>\n\n<p>You have a fun task ahead of you my friend. Best of luck, and please ask if you need anything more :)</p>\n'}, {'answer_id': 15030, 'author': 'Adam V', 'author_id': 517, 'author_profile': 'https://Stackoverflow.com/users/517', 'pm_score': 1, 'selected': False, 'text': "<blockquote>\n  <p>You are most likely being set up to fail. Welcome to the industry.</p>\n</blockquote>\n\n<p>I disagree - so long as he creates the document, the worst that can happen is that it gets forgotten by everyone.</p>\n\n<p>If other people have issues with the content, then you can ask them to update it to show what they'd prefer. That way it's off your plate, and the others have the responsibility to justify their changes.</p>\n"}, {'answer_id': 15039, 'author': 'David Hayes', 'author_id': 1769, 'author_profile': 'https://Stackoverflow.com/users/1769', 'pm_score': 3, 'selected': False, 'text': "<p>Never write your own coding standards use the MS ones (or the Sun ones or ... as appropriate for your language). The clue is in the word standard, the world would be a much easier place to code in if each organization hadn't decided to write their own. Who really thinks learning a new set of 'standards' each time you change teams/projects/roles is a good use of anyone's time.\nThe most you should ever do is summarize the critical points but I'd advise against doing even that because what is critical varies from person to person.\nTwo other points I'd like to make on coding standards</p>\n\n<ol>\n<li>Close is good enough - Changing code to follow coding standards to the letter is a waste of time as long as the code is close enough.</li>\n<li>If you're changing code you didn't write follow the 'local coding standards', i.e. make your new code look like the surrounding code.</li>\n</ol>\n\n<p>These two points are the reality to my wish that everybody would write code that looked the same.</p>\n"}, {'answer_id': 85305, 'author': 'Joe', 'author_id': 13087, 'author_profile': 'https://Stackoverflow.com/users/13087', 'pm_score': 0, 'selected': False, 'text': '<p>The standard from Philips Medical Systems is well written, and mostly follows Microsoft guidelines: www.tiobe.com/content/paperinfo/gemrcsharpcs.pdf</p>\n\n<p>My standards are based on this with a few tweaks, and some updates for .NET 2.0 (the Philips standard is written for .NET 1.x so is a bit dated).</p>\n'}, {'answer_id': 113139, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>I would be tempted to enforce Microsoft\'s StyleCop as the standard.  It can be enforced at the build time.  but if you have legacy code then just enforce using StyleCop on new code.</p>\n\n<p><a href="http://code.msdn.microsoft.com/sourceanalysis" rel="nofollow noreferrer">http://code.msdn.microsoft.com/sourceanalysis</a></p>\n\n<p>Eventually it will have a refactor option to cleanup code.</p>\n\n<p><a href="http://blogs.msdn.com/sourceanalysis/" rel="nofollow noreferrer">http://blogs.msdn.com/sourceanalysis/</a></p>\n'}, {'answer_id': 113159, 'author': 'samiq', 'author_id': 1549, 'author_profile': 'https://Stackoverflow.com/users/1549', 'pm_score': 3, 'selected': False, 'text': '<p>I would add <a href="https://rads.stackoverflow.com/amzn/click/com/0735619670" rel="nofollow noreferrer" rel="nofollow noreferrer">Code Complete 2</a> to the list (I know Jeff is kind of a fan here)... If you are a junior developer, the book comes in handy to set up your mind in a way that sets the foundation for the best code writing practices and software building there are.</p>\n\n<p>I have to say that I came to it a bit late in my career, but it rules a lot of the ways I think about coding and framework development in my professional life.</p>\n\n<p>It\'s worth checking out ;)</p>\n'}, {'answer_id': 244317, 'author': 'alexandrul', 'author_id': 19756, 'author_profile': 'https://Stackoverflow.com/users/19756', 'pm_score': 1, 'selected': False, 'text': '<p>I have recently found <a href="http://code.msdn.microsoft.com/encodocsharphandbook/Wiki/View.aspx" rel="nofollow noreferrer">Encodo C# Handbook</a>, which includes ideas from many other sources (IDesign, Philips, MSDN). </p>\n\n<p>Another source may be <a href="http://submain.com/?nav=products.guidelines" rel="nofollow noreferrer">Professional C#/VB .NET Coding Guidelines</a>.</p>\n'}, {'answer_id': 250590, 'author': 'Abel Gaxiola', 'author_id': 31191, 'author_profile': 'https://Stackoverflow.com/users/31191', 'pm_score': 3, 'selected': False, 'text': '<p>I found the following documentation very helpful and concise.  It comes from the idesign.net site and it is authored by Juval Lowy</p>\n\n<p><a href="http://idesign.net/idesign/download/IDesign%20CSharp%20Coding%20Standard.zip" rel="nofollow noreferrer">C# Coding Standard</a></p>\n\n<p>NB: the above link is now dead.  To get the .zip file you need to give them your email address (but they won\'t use it for marketing... honestly)  Try <a href="http://idesign.net/Downloads" rel="nofollow noreferrer">here</a></p>\n'}, {'answer_id': 250636, 'author': 'urini', 'author_id': 373, 'author_profile': 'https://Stackoverflow.com/users/373', 'pm_score': 1, 'selected': False, 'text': '<p>I\'m a big fan of the Francesco Balena book "<a href="http://www.dotnet2themax.com/PracticalGuidelines.aspx" rel="nofollow noreferrer">Practical Guidelines and Best Practices for VB  and C# Developers</a>".</p>\n\n<p>It\'s very detailed and covers all the essential topics, It doesn\'t just give you the rule, but also explains the reason behind the rule, and even provides an anti-rule where there could be two opposing best practices. The only downside is that it was written for .NET 1.1 developers.</p>\n'}, {'answer_id': 325800, 'author': 'Pradeep Kumar Mishra', 'author_id': 22710, 'author_profile': 'https://Stackoverflow.com/users/22710', 'pm_score': 1, 'selected': False, 'text': '<p>See this:\n<a href="http://www.noesispedia.com/post/2008/11/28/C-Coding-Guidelines-and-Best-Practices.aspx" rel="nofollow noreferrer">http://www.noesispedia.com/post/2008/11/28/C-Coding-Guidelines-and-Best-Practices.aspx</a>.</p>\n'}, {'answer_id': 709221, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 3, 'selected': False, 'text': "<p>I've just started at a place where the coding standards mandate the use of m_ for member variables, p_ for parameters and prefixes for types, such as 'str' for strings.\nSo, you might have something like this in the body of a method:</p>\n\n<p>m_strName = p_strName;</p>\n\n<p>Horrible.  Really horrible.</p>\n"}, {'answer_id': 1198302, 'author': 'derGral', 'author_id': 136596, 'author_profile': 'https://Stackoverflow.com/users/136596', 'pm_score': 2, 'selected': False, 'text': '<p>Personally I like the one that <a href="http://www.idesign.net/idesign/download/IDesign%20CSharp%20Coding%20Standard.zip" rel="nofollow noreferrer">IDesign</a> has put together.  But that\'s not why I\'m posting...</p>\n\n<p>The tricky bit at my company was taking all the different languages into account.  And I know my company isn\'t alone on this.  We use C#, C, assembly (we make devices), SQL, XAML, etc.  Although there will be some similarities in standards, each is usually handled differently.   </p>\n\n<p>Also, I believe that higher level standards have a greater impact on the quality of the final product.  For example: how and when to use comments, when exceptions are mandatory (e.g. user initiated events), whether (or when) to use exceptions vs. return values, what is the objective way to determine what should be controller code vs presentation code, etc. Don\'t get me wrong, low level standards are also needed (formatting is important to readability!) I just have a bias towards overall structure.</p>\n\n<p>Another piece to keep in mind is buy-in and enforcement.  Coding standards are great.  But if nobody agrees with them and (probably more importantly) no one enforces them then it\'s all for naught.</p>\n'}, {'answer_id': 1200118, 'author': 'John Kraft', 'author_id': 7495, 'author_profile': 'https://Stackoverflow.com/users/7495', 'pm_score': 1, 'selected': False, 'text': '<p>Our entire coding standard reads roughly, "Use StyleCop."</p>\n'}, {'answer_id': 2957904, 'author': 'the_drow', 'author_id': 85140, 'author_profile': 'https://Stackoverflow.com/users/85140', 'pm_score': 1, 'selected': False, 'text': '<p>I have to suggest the <a href="http://www.dotnetspider.com/tutorials/bestpractices.aspx" rel="nofollow noreferrer">dotnetspider.com</a> document.<br>\nIt is a great and detailed document that is useful anywhere.</p>\n'}, {'answer_id': 5732930, 'author': 'kenny', 'author_id': 3225, 'author_profile': 'https://Stackoverflow.com/users/3225', 'pm_score': 1, 'selected': False, 'text': '<p>I\'ve used Juval\'s before and that\'s through if not overkill, but I\'m lazy and now just conform to the will of <a href="http://www.jetbrains.com/resharper/" rel="nofollow">Resharper</a>. </p>\n'}, {'answer_id': 6120632, 'author': 'Kamran Shahid', 'author_id': 578178, 'author_profile': 'https://Stackoverflow.com/users/578178', 'pm_score': 0, 'selected': False, 'text': '<p>I also follow Resharper.\nAlso the guide line mentioned on scott guthrie blog \n<a href="http://weblogs.asp.net/scottgu/archive/2007/10/08/october-8th-links-asp-net-asp-net-ajax-silverlight-and-net.aspx" rel="nofollow">http://weblogs.asp.net/scottgu/archive/2007/10/08/october-8th-links-asp-net-asp-net-ajax-silverlight-and-net.aspx</a>\nAnd \n<a href="http://csharpguidelines.codeplex.com/releases/view/46280" rel="nofollow">http://csharpguidelines.codeplex.com/releases/view/46280</a></p>\n'}, {'answer_id': 6120801, 'author': 'Dan Abramov', 'author_id': 458193, 'author_profile': 'https://Stackoverflow.com/users/458193', 'pm_score': 0, 'selected': False, 'text': '<p>In the code I write I usually follow <strong><a href="http://msdn.microsoft.com/en-us/library/ms229042.aspx" rel="nofollow">.NET Framework Design Guidelines</a> for publicly exposed APIs and <a href="http://www.mono-project.com/Coding_Guidelines" rel="nofollow">Mono Coding Guidelines</a> for private member casing and indentation</strong>. Mono is an open source implementation of .NET, and I think those guys know their business.</p>\n\n<p>I hate how Microsoft code wastes space:</p>\n\n<pre><code>try\n{\n    if (condition)\n    {\n        Something(new delegate\n        {\n            SomeCall(a, b);\n        });\n    }\n    else\n    {\n        SomethingElse();\n        Foobar(foo, bar);\n    }\n}\ncatch (Exception ex)\n{\n    Console.WriteLine("Okay, you got me");\n}\n</code></pre>\n\n<p>What you might find strange in Mono guidelines, is that they use 8-space tabs. However, after some practice, I found that it actually helps me writing less tangled code by enforcing a kind of indentation limit.</p>\n\n<p>I also love how they put a space before opening parenthesis.</p>\n\n<pre><code>try {\n        if (condition) {\n                Something (new delegate {\n                        SomeCall (a, b);\n                });\n        } else {\n                SomethingElse ();\n                Foobar (foo, bar);\n        }\n} catch (Exception ex) {\n        Console.WriteLine ("Okay, you got me");\n}\n</code></pre>\n\n<p>But please, don\'t enforce anything like that if your coworkers dislike it (unless you are willing to contribute to Mono ;-)</p>\n'}, {'answer_id': 8529243, 'author': 'Elaine', 'author_id': 343117, 'author_profile': 'https://Stackoverflow.com/users/343117', 'pm_score': 1, 'selected': False, 'text': '<p>You can check out this,Top 7 Coding Standards &amp; Guideline Documents For C#/.NET Developers <a href="http://www.amazedsaint.com/2010/11/top-6-coding-standards-guideline.html" rel="nofollow">http://www.amazedsaint.com/2010/11/top-6-coding-standards-guideline.html</a>  hope this helps</p>\n'}, {'answer_id': 8933271, 'author': 'Dennis Doomen', 'author_id': 253961, 'author_profile': 'https://Stackoverflow.com/users/253961', 'pm_score': 2, 'selected': False, 'text': '<p>As I wrote both the one published for Philips Medical Systems and the one on <a href="http://csharpguidelines.codeplex.com" rel="nofollow">http://csharpguidelines.codeplex.com</a> I might be a bit biased, but I have more than 10 years on writing, maintaing and the promotion of coding standards. I\'ve tried to write the one CodePlex with differences in opinions in mind and spent the majority of the introduction on how to deal with that in your particular organisation. Read it and provide me with feedback.....</p>\n'}, {'answer_id': 9239618, 'author': 'Adam Cogan', 'author_id': 305089, 'author_profile': 'https://Stackoverflow.com/users/305089', 'pm_score': 2, 'selected': False, 'text': '<p><a href="http://www.ssw.com.au/ssw/standards" rel="nofollow">SSW Rules</a> </p>\n\n<p>It includes some C# standards + a whole lot more.... primarily focused at Microsoft developers</p>\n'}]
['https://Stackoverflow.com/questions/14967', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1816/']