Share via


Announcing the release of Microsoft StyleCop

We are very excited to announce the release of a new developer tool from Microsoft, Source Analysis for C#. This tool is known internally within Microsoft as StyleCop, and has been used for many years now to help teams enforce a common set of best practices for layout, readability, maintainability, and documentation of C# source code.

StyleCop is similar in many ways to Microsoft Code Analysis (specifically FxCop), but there are some important distinctions. FxCop performs its analysis on compiled binaries, while StyleCop analyzes the source code directly. For this reason, FxCop focuses more on the design of the code, while StyleCop focuses on layout, readability and documentation. Most of that information is stripped away during the compilation process, and thus cannot be analyzed by FxCop.

The ultimate goal of StyleCop is to allow you to produce elegant, consistent code that your team members and others who view your code will find highly readable. In order to accomplish this, StyleCop does not allow its rules to be very configurable. StyleCop takes a one-size-fits-all approach to code style, layout, and readability rules. It is highly likely that you will not agree with all of the rules and may even find some of the rules annoying at first! However, the majority of teams using this tool within Microsoft have found that after a short adjustment period, they came to appreciate the rules enforced by StyleCop, and even began to find it difficult to read code not written in this style. 

StyleCop comes with a set of default rules analyzers covering approximately 200 best practice rules. These rules are full compatible with the default layout settings in Visual Studio 2005 and Visual Studio 2008.

Specifically, these rules cover the following, in no particular order:

  • Layout of elements, statements, expressions, and query clauses
  • Placement of curly brackets, parenthesis, square brackets, etc
  • Spacing around keywords and operator symbols
  • Line spacing
  • Placement of method parameters within method declarations or method calls
  • Standard ordering of elements within a class
  • Formatting of documentation within element headers and file headers
  • Naming of elements, fields and variables
  • Use of the built-in types
  • Use of access modifiers
  • Allowed contents of files
  • Debugging text

After installation, StyleCop can be run from within the Visual Studio IDE, and can also be integrated into MSBuild-based command line builds.

StyleCop can be downloaded here: https://code.msdn.microsoft.com/Release/ProjectReleases.aspx?ProjectName=sourceanalysis. We're looking forward to hearing your feedback!

Comments

  • Anonymous
    May 23, 2008
    I've been using StyleCop within Microsoft for some time now. I'm a big fan, and really excited to see

  • Anonymous
    May 23, 2008
    Announcing the release of Microsoft Source Analysis for C#Source Analysis is similar in many ways to...

  • Anonymous
    May 23, 2008
    Excerpt from the post... We are very excited to announce the release of a new developer tool from Microsoft

  • Anonymous
    May 23, 2008
    Microsoft відповіла тулзам, що використовують парсінг коду. Офіційний анонс - http://blogs.msdn.com/sourceanalysis/archive/2008/05/23/announcing-the-release-of-microsoft-source-analysis.aspx

  • Anonymous
    May 23, 2008
    You've been kicked (a good thing) - Trackback from DotNetKicks.com

  • Anonymous
    May 23, 2008
    The comment has been removed

  • Anonymous
    May 23, 2008
    The comment has been removed

  • Anonymous
    May 23, 2008
    This is a great thing for MS to release to the developer community - StyleCop is a very interesting tool

  • Anonymous
    May 23, 2008
    But what about vb.net? Why are all these new fancy tools being released for C#? Do VB.net developers not need the kind of analysis like C# or does every one write sloppy C# code?

  • Anonymous
    May 23, 2008
    Yes we are contemplating adding auto fixing for a future release.

  • Anonymous
    May 23, 2008
    The comment has been removed

  • Anonymous
    May 23, 2008
    Is there documentation for what it checks, or at least a document I can reference with the rules? Most coding standards I have seen disagree with the rules. For example, using spaces instead of tabs. Tabs can be resized for preference, spaces cannot.

  • Anonymous
    May 23, 2008
    The company I work for is stuck on VB.net. Is there any plan to have a VB.net version of this tool?

  • Anonymous
    May 23, 2008
    The comment has been removed

  • Anonymous
    May 23, 2008
    I would find it very helpful if it was configurable. I work for various clients and each has their own coding standards and conventions that they dictate to me and the development team here. It would be great to associate a project with a customized set of rules that vary from the standard so that I can deliver the code in the way the client expects and wants. Beyond that, any open source work I do in C# would conform to the strict standard. Another one I have used it Code Style Enforcer which does allow you to customize the rules. http://joel.fjorden.se/static.php?page=CodeStyleEnforcer

  • Anonymous
    May 23, 2008
    Are you kidding me.  Tabs are not allowed.  Use spaces instead.  This is a horrible idea, because it throws blocking layout off, if one variable uses 3 spaces and the other uses 4.  This there anyway to disable some of the more pointless rules like this tab one.

  • Anonymous
    May 23, 2008
    The comment has been removed

  • Anonymous
    May 23, 2008
    anyone know how to add a header file to their c# documents?

  • Anonymous
    May 23, 2008
    FYI - The code site is now offline.

  • Anonymous
    May 23, 2008
    El día de hoy ha sido liberado Microsoft Source Analysis para C# , la cual es una herramienta que nos

  • Anonymous
    May 23, 2008
    El día de hoy ha sido liberado Microsoft Source Analysis para C# , la cual es una herramienta que nos

  • Anonymous
    May 23, 2008
    This is a very welcome addition to the set of tools available for .NET developers - but why only for C#? Enforcement of code formatting rules is just as sorely needed for VB developers! Admittedly, the VB code editor has some rudimentary support for automatic formatting when it comes to indentation and spacing around keywords and symbols, but nothing that even comes close to what StyleCop supports. By the way, I totally agree with "nberardi" on the tabs vs. spaces issue: what is the problem with tabs? Is it somehow an advantage to have to press the arrow key 4-5 times more to get to a given point? Is it somehow an advantage to have to store 4-5 times as many whitespace characters in a source file?

  • Anonymous
    May 23, 2008
    People just love to write code. One tool that is used inside Microsoft is StyleCop which enforces code

  • Anonymous
    May 23, 2008
    Amazing news!  ... downloading...

  • Anonymous
    May 23, 2008
    Some of these rules do seem quite silly to me. Particularly SA1500, What do you have against Java style formatting? I find it looks a lot nicer that way. (In fact, there's an option for it in Visual Studio, should you really penalize somebody for something that you let them autoformat it to?)

  • Anonymous
    May 23, 2008
    最近微软发布了一款代码分析工具,和微软内部使用的FxCop有些不同,目的大致一样,其工具的规则覆盖到: Layoutofelements,statements,expressions...

  • Anonymous
    May 23, 2008
    引言:SourceAnalysis(StyleCop)的终极目标是让所有人都能写出优雅和一致的代码,因此这些代码具有最高的可读性。早就听说了微软内部的静态代码检查和代码强制格式美化工具Style...

  • Anonymous
    May 23, 2008
    The comment has been removed

  • Anonymous
    May 23, 2008
    The comment has been removed

  • Anonymous
    May 23, 2008
    File header documentation is now available, at http://blogs.msdn.com/sourceanalysis/pages/source-analysis-4-2-documentation-rules.aspx

  • Anonymous
    May 23, 2008
    Microsoft announces Source Analysis for C#. It is very similar to Static Code Analysis (FxCop), but instead

  • Anonymous
    May 23, 2008
    The comment has been removed

  • Anonymous
    May 23, 2008
    Jason Allor anunciaba ayer mismo el lanzamiento de una nueva herramienta, Microsoft Source Analysis for

  • Anonymous
    May 23, 2008
    The comment has been removed

  • Anonymous
    May 24, 2008
    For the people who want it to be more configurable -- the lack of configuration is the big point.  This is an internal tool that they released because it is useful, not a product they sell.  The, obvious, initial purpose is to make all microsoft code look like microsoft code.  Configurability would have defeated it purpose. Now if microsoft sends you a paycheck every other week, then they have the right to tell you what your code will look like -- by any means they choose.  If they don't, then code however you like, and don't use the free tool if you don't like it.   Think of this as a gift from devdiv.  If you want to live by the rules of the castle, enjoy the consistency it provides.  If you don't, please don't scorn microsoft for allowing us to use their internal tools for free without buffing them to consumer quality.

  • Anonymous
    May 24, 2008
    For the VB'ers out there, you can always try VS Law which does much the same as StyleCop but unlike StyleCop is fully configurable (you can switch rules off an on, you can even create your own).  At the moment it is not free, but as the authors (I am one) no longer have the time to keep up it up to date with the language developments Microsoft are releasing, we are considering making it open source.  Is this of interest to anyone?

  • Anonymous
    May 24, 2008
    To all the people who desires more configuration option I would like to point out that the tool already supports this: Right click on the project node in the solution explorer and choose the menu item "Source Analysis Settings". There you can customize which rules to run.

  • Anonymous
    May 24, 2008
    Nagyon rendes a Microsoft-tól hogy megosztja a fejlesztőkkel az egyik belső fejlesztői toolját. A Microsfton

  • Anonymous
    May 24, 2008
    Microsoft released the internal tool StyleCop to public under the fancy yet boring name of Microsoft

  • Anonymous
    May 24, 2008
    The tool is nice and all, but when run on a Windows Application project, it complains about how the Form designer files are laid out. Now - those files are generated automatically. Why am I supposed to go in and edit the layout in them?

  • Anonymous
    May 24, 2008
    Microsoft has published a great tool called Microsoft Source Analysis for C# , also known as StyleCop

  • Anonymous
    May 24, 2008
    "...when run on a Windows Application project, it complains about how the Form designer files are laid out. Now - those files are generated automatically. Why am I supposed to go in and edit the layout in them?" There is a difference between auto-generated code that will later be overwritten again by a tool, and auto-generated code that will never again be overwritten. In the Forms case, the code is originally created for you by a tool, but after that you own the code and it's up to you to maintain it. There is a special region at the bottom of that file containing the code that can actually be overwritten again by the tool, and StyleCop will ignore that part of the code.

  • Anonymous
    May 24, 2008
    rnHelps teams enforce a common set of best practices for layout, readability, maintainability, and

  • Anonymous
    May 24, 2008
    Integration continue : Partie 4, Microsoft Source Analysis Tool for C#

  • Anonymous
    May 24, 2008
    Integration continue : Partie 4, Microsoft Source Analysis Tool for C#

  • Anonymous
    May 25, 2008
    Un nouvel outil est disponible pour les développeurs C#. Comme son nom l'indique il s'agit d'un analyseur

  • Anonymous
    May 25, 2008
    I will need more time to go through the tool (just downloaded it), but already can see few little issues:

  1. Violation SA1027 (Tabs are not allowed. Use spaces instead) is definitely not making any sense. Tabs have many advantages compared to spaces, but the most important one is that each team member can set tab's width according to his personal preference (I prefer 3, someone else - 4, 2, or even 8!), and there is no need for "religious wars" because of this non-issue.
  2. Source Analisys tab (the one listing violations) should have numbered lines - it is very difficult to navigate in the list if it has more items.
  3. As some people already mentioned above, the tool has very limited applicability if the user has to fix violations manually. Some kind of automatic fixing (e.g. via context menu in the Source Analisys tab) is absolutely must. Otherwise "fixing" existing codebase is really not an option - in simple (25 lines of not that bad code!) file the tool found 28 "violations" (from which 16 were "tabs").
  • Anonymous
    May 25, 2008
    Hi,    With FxCop and this program, my codes are even better. But i suggestion, maybe do a program like that to WCF Contracts and Services, to create better WCF Services mainly because SOA, the exposed methods are growing. Regards,          Alan - Enterprise Architect

  • Anonymous
    May 25, 2008
    Hi,    With FxCop and this program, my codes are even better. But i suggestion, maybe do a program like that to WCF Contracts and Services, to create better WCF Services mainly because SOA, the exposed methods are growing. Regards,          Alan - Enterprise Architect

  • Anonymous
    May 25, 2008
    It says it will help your code to be more "elegant, consistent..." so naturally it must work with C#. I wish I had a dollar for every library, tool, whitepaper, msdn article, etc. that came out of MS for C# only.

  • Anonymous
    May 25, 2008
    The comment has been removed

  • Anonymous
    May 25, 2008
    Som thomas skriver, så har Microsoft udgive et værktøj til analyse af din kode - og som sædvanlig er

  • Anonymous
    May 25, 2008
    IMHO rule SA1600 should be split up in private members and members with a higher visibility. I get a lot of SA1600 "errors" because I did not write comments for my private fields: Error SA1600: The field must have a documentation header.   [Note from jasonall - This can be configured by bringing up Source Analysis project settings, navigating to Rules tab, and selecting the Documentation node. A detailed setting allows you to control this]

  • Anonymous
    May 25, 2008
    On <a href="http://wojciech.zimirski.net/2008/5/24/microsoft-source-analysis-for-c"> my blog</a> you can find list of all rules analysed by the tool.

  • Anonymous
    May 26, 2008
    It would be nice to have some global settings or at least solution-wide settings. For tabs vs. spaces, it would be nice to be easily able to remove it for our whole company. [Note from jasonall - It is  possible to configure settings for all projects, or to share settings across multiple projects. Check the following post: http://blogs.msdn.com/sourceanalysis/pages/sharing-source-analysis-settings-across-projects.aspx]  

  • Anonymous
    May 26, 2008
    Microsoft announce the public release of a new developer tool -&#160; Source Analysis for C# . Inside

  • Anonymous
    May 26, 2008
    The static analysis tool FXCop, and its later relation Visual Studio Code Analysis, have been well known

  • Anonymous
    May 27, 2008
    The comment has been removed

  • Anonymous
    May 27, 2008
    The community would obviously like more configurability than Microsoft needs internally. Any possibility of releasing the source so the community can add the configurability?

  • Anonymous
    May 27, 2008
    Jsaon, Several people (including me) asked about the availability of this tool for VB.NET. It would be nice if this question was not ignored. Even if we get the usual "no support for vb.net planned at the moment" type of response, at least we'll know where we stand Thank you

  • Anonymous
    May 28, 2008
    Exsits any similar tool for C++ ? Thanks

  • Anonymous
    May 28, 2008
    The reason Microsoft won't have a similar tool for VB.Net is that internally they don't use VB.Net for anything - all their .Net coding efforts are in C#

  • Anonymous
    May 29, 2008
    When I left Microsoft, I felt pretty sad. Not only would I leave a bunch of great friends behind (and

  • Anonymous
    May 29, 2008
    After almost two months working on this , things are finally falling on their correct place. Today the

  • Anonymous
    May 29, 2008
    /// <summary> /// Sample method to add two number together /// </summary> /// <param name="first"></param> /// <param name="second"></param> /// <returns>sum of the two params passed</returns> public double AddTwoNumbers(double first, double second) { return first + second; } Any ideas why I get SA1614 on this?   [jasonall] You are missing documentation text for the 'first' and 'second' parameters.

  • Anonymous
    May 29, 2008
    I don't like with the coding standard this tool enforces.. it gives me the "enterprise developer" chills..

  • Anonymous
    June 01, 2008
    It would be nice to see a VB.NET version. Judging from the posts here, there is certainly some interest.

  • Anonymous
    June 01, 2008
    Considering SA1633: FileMustHaveHeader I think that file header should start with 3 slashes (///) not 2 (//) to be consistent to other documentation comments generated in Visual Studio.

  • Anonymous
    June 01, 2008
    I'd like to have a context menu entry as we can find in the regular build error list "Show Error Help" which links to a brief description of the specific analysis error (which means integration into the VS help Database). That would be way more convenient than having to do a MSDN search.

  • Anonymous
    June 02, 2008
    ASP.NET ASP.NET MVC Preview 3 : It just keeps getting better and better, folks! Scott Guthrie shares all the juicy details on the latest drop of this fantabulous toolkit! Using jQuery To Call ASP.NET AJAX Page Methods : Not a fan of ASP.NET AJAX? Love

  • Anonymous
    June 02, 2008
    My learned colleague Howard van Rooijen recently blogged about the new release of Microsoft Source Analysis

  • Anonymous
    June 02, 2008
    I have no detailed section for: SA1600: The field must have a documentation header. If I open up Source Analysis project settings, navigate to Rules tab, and select the Documentation node, the detailed section is empty.

  • Anonymous
    June 04, 2008
    podi, you're looking for NArrange: http://www.codeproject.com/KB/codegen/narrange.aspx

  • Anonymous
    June 04, 2008
    I’ve learned from a fellow GASP er of the release of Microsoft Source Analysis for C# (aka StyleCop)

  • Anonymous
    June 04, 2008
    I’ve learned from a fellow GASP er of the release of Microsoft Source Analysis for C# (aka StyleCop)

  • Anonymous
    June 04, 2008
    The comment has been removed

  • Anonymous
    June 05, 2008
    Announcing the release of Microsoft Source Analysis for C#

  • Anonymous
    June 05, 2008
    Continuous Integration Part 3 : Integrating Microsoft Source Analyzer Tool to our build process

  • Anonymous
    June 11, 2008
    Might be a great tool. It would be better if you hadn't made it so 'tightly coupled' to C#. Couldn't you have just wrote a core engine and then allowed each language team to write an add-in to make it work for other languages, along with a best practice ruleset for each language which could be altered as needed by individual shops using the tool in order to fit with their internal coding standards(which may not always match Microsoft's)? 8/10 for effort. Could try harder.

  • Anonymous
    June 18, 2008
    For the past couple of years, I spent considerable time saying the same statement over and over again

  • Anonymous
    June 24, 2008
    If you do have Microsoft Source Analysis Tools for C# and JetBrains ReSharper 4.0 installed in you development

  • Anonymous
    June 25, 2008
    h2.entry-title { font-size: 1.1em; clear: left; } ul.hfeed { list-style-type: none; } li.xfolkentry

  • Anonymous
    June 26, 2008
    I&#39;d like to collect some good links about Team System from the community and Microsoft: MSBuild tasks

  • Anonymous
    June 29, 2008
    Hi. Does this tool work with express editions? Just a doubt, because I tryied at home using express ed. and could not find the option. Thanks!

  • Anonymous
    July 02, 2008
    I would love to see a VB.NET version of this tool.

  • Anonymous
    July 03, 2008
    I would also like to see StyleCop for VB.Net. …Yes, I read that many of you asked for it, but I anyway decided to write, as I hope that if Microsoft sees how many people demand the tool for VB.Net they might consider adapting it for us. Thanks.

  • Anonymous
    July 03, 2008
    Great idea, however, it is absolutly nessessary that youu build in an option to ommit rules that do not apply to your code. I.e. In my company we are 100% agile, thus our unittests are our documentation. We do not want 1000 errors just because we havn't written 1.000.000 lines of code comments. Second, some of the formatting rules are in conflict with production-enhancing tools like resharper (i.e. where using statements should be). As much as i like the MSCop-idea, I would NEVER trade my resharper for MSCop.

  • Anonymous
    July 07, 2008
    Wishlist request: turn this into a standalone tool, separated from VS. Reason: I have been bit (as have others) by the dreaded "Package 'Visual Studio Explorers and Designers Package' has failed to load properly" error in VS after using this add-in.

  • Anonymous
    July 09, 2008
    Any idea on the release date of version 4.3? Thanks :)

  • Anonymous
    August 04, 2008
    The comment has been removed

  • Anonymous
    August 12, 2008
    Should we expect the release in September? Ray Akkanson

  • Anonymous
    September 22, 2008
    What about VB.NET! Our company insists on VB.Net and i think we could use this tool for our commenting addle devs... I saw the comments for VB.NET but as many people asking for it, it will be developed once...

  • Anonymous
    September 23, 2008
    Just i went through the topic,thank u so much

  • Anonymous
    October 13, 2008
    Please use the StyleCop Discussion Forum for further comments as it provides a better mechanism for tracking topics and follow-ups.

  • Anonymous
    May 26, 2009
    The comment has been removed