12

We have 3 years old solution (.sln) with about 20 projects (.csproj). It is reasonable to start using FxCop and/or StyleCop? Maybe we should use it for several small projects first but not for whole solution?

It would be good to see some experienced answers.

Thanks.

EDIT: We are using TeamCity for continuous integration. And we have no possibility to use ReSharper. :( CodeRushXpress only.

Vasyl Boroviak
  • 5,959
  • 5
  • 51
  • 70

6 Answers6

10

Yes, you should, but slowly.

Get a copy of ReSharper, install StyleCop, and get the StyleCop for ReSharper plugin, setup which rules you'd like to use, and from then on every file you open will be full of wiggly blue lines to tell you where things are bad.

If you just fix them, one file at a time, you'll eventually end up with a nice clean project, without the need to convince your boss to let you spend 3 weeks going through your project doing nothing that results in chargable time!

Getting clean code is like refactoring, if you try and do it over an entire project all at once, you're going to end up in a pickle :)

Ed James
  • 10,385
  • 16
  • 71
  • 103
  • Yeah, Adding them will KILL you for some time - it will show tons of issues. – TomTom Mar 17 '10 at 09:24
  • Have no possibility to use ReSharper. :( Any other ways to use StyleCop? Thru CodeRushXPress maybe? And what about FxCop? – Vasyl Boroviak Mar 17 '10 at 09:33
  • 1
    Found StyleCop for CodeRushXpress! http://crstylecop.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=30407 Thanks for the idea. – Vasyl Boroviak Mar 17 '10 at 09:38
  • Hmm, didn't know about that. I'm not a big fan of CodeRush, the main reason I have ReSharper is because of the syntax highlighting, and the little lightbulb thingy that pops up with refactoring and error-correcting options. CodeRush didn't seem to have that. A few of the other guys here use it though, so I might point them towards that addon :D – Ed James Mar 17 '10 at 09:46
  • 6
    First use stylecop on individual files to resolve most issues and THEN install stylecop for resharper - else you will get flooded by the issues. – Hans Kesting Mar 17 '10 at 09:47
  • I've got 50 StyleCop warnings on a single 100 lines file. :) Looks like we have to do it file by file as Hans suggested. Too pity it is a comment but not answer. I would choose it as the correct answer to my question. – Vasyl Boroviak Mar 17 '10 at 12:06
  • To be fair, I did say one file at a time ;) But yes, you'll find that the first few times you run it you'll get a large volume of warnings, just keep it up and eventually you'll end up with a nice codebase that's easier to maintain! – Ed James Mar 17 '10 at 15:25
3

An alternative or a good complement to FxCop/StyleCop would be to use the commercial tool NDepend. With this tool one can write Code Rule over LINQ Queries (namely CQLinq). Disclaimer: I am one of the developers of the tool

More than 200 code rules are proposed by default, these include design, architecture, code quality, code evolution, naming conventions, dead code, .NET Fx usage...

CQLinq is dedicated to write code rules that can be verified live in Visual Studio, or that can be verified during build process and reported in an HTML/javascript report.

The strength of CQLinq over FxCop or StyleCop, is that it is straightforward to write a code rule, and get immediately results. Facilities are proposed to browse matched code elements. Concretely this looks like that:

CQLinq code rule

Patrick from NDepend team
  • 13,237
  • 6
  • 61
  • 92
2

I've just started using StyleCop on my personal projects and it does take a little time to work through the "issues" raised.

I would recommend running StyleCop on a sample of your files and analysing the results before launching in to make any changes.

For example, by default StyleCop complains about missing method documentation for all methods, both public and private. Now, I can't answer this for you, but you need to decide whether you want private methods to have full documentation or not (there are arguments both ways). But you need to decide one way or the other. You don't want to get 6 months into making changes one way and then decide that you want it the other. That's either going to lead you to make unnecessary changes or have to revisit code you thought you'd finished.

Once you have make the necessary adjustments to StyleCop's settings and then let it loose on your code base - one project at a time.

ChrisF
  • 134,786
  • 31
  • 255
  • 325
  • I suppose there is the way to use StyleCop for single project from the colution but not for whole solution. Thanks. – Vasyl Boroviak Mar 17 '10 at 09:40
  • @Vasiliy - I know you can right click over a file and run StyleCop on just that file from the context menu. Can you do the same by right clicking over a project? (I haven't got StyleCop installed on this PC to check). – ChrisF Mar 17 '10 at 09:44
  • Just installed StyleCop. Yes, it is possible to run on a file or a project. – Vasyl Boroviak Mar 17 '10 at 10:26
  • @Vasiliy - I thought you could run it on an individual project, but I wasn't 100% sure so didn't want to make a categorical statement. – ChrisF Mar 17 '10 at 11:10
1

Regarding FxCop, yes it is a good idea to make use of the tool, whether you are in a new project or an existing one. Much like StyleCop, you can run the tool and review the output. Unlike StyleCop, FxCop works on compiled code, not source.

It will probably be overwhelming at first. A good idea is to turn off all the rule groups, and rerun the tool to get a blank slate. Enable one group at a time, resolving any messages that appear, or turning off specific rules in that group if they do not apply to you (the default rules as a whole are quite broad, and not all will apply; you need to customize the ruleset for your needs).

At the end, you will have resolved all messages by either implementing appropriate fixes, or selectively disabling extraneous rules.

As a rule (no pun intended) I consider the Security and Performance groups good ones to start with. The Naming rules are subjective, and may clash with your own conventions. Turn them off if so. Mobility and Globalization are also subjective, and depend on your needs. As for the rest, well, you create your own conclusions!

Grant Palin
  • 4,546
  • 3
  • 36
  • 55
0

Depending on how solution was created integration (and fixing issues reported by these tools) could take a lot of time, so I think integration of projects one by one is the way you should go.

EDIT:

In addition to Ed Woodcock's answer: you can configure SyleCop (and I bet FxCop too) to automatically run their checks during each VS build. Using this feature you can turn on checks on all of your projects one by one and fix all warnings (also you can configure these tools to generate errors) just during your regular development.

Andrew Bezzub
  • 15,744
  • 7
  • 51
  • 73
0

It depends:

  • There is no value in changing code that is working for the sake of it.
  • The StyleCop coding style may not match well with the style in your current code.
  • You are likely to introduce bugs if you try to get your current code to pass FxCop and/or StyleCop
  • A lot of the FxCop rules are of little or no value unless 3rd parties are writing code again your dlls.
  • There is no point in using a checking tool if you ignore 101 warnings from it, as you will never spot the important one you care about.

I think it comes down to the possible reduction in the cost of maintaining your current code base compared to the cost of getting the code base to pass the checking tools. A more consistent code base will have a lower cost of maintenance, but this is only of value if you are making lots of changes to the code base.

Ian Ringrose
  • 51,220
  • 55
  • 213
  • 317
  • I feel this comment passes on the incorrect fact that working code is good code, it may not be. There is immense value to good code. 85% of time is spent on maintenance, good code improves maintenance. There is huge value in changing code that is working, more so then code that is not! Code that is working may be left for long periods of time, it should be documented and documented well. It should follow a set of standards for style and design you plan on using going forward, and more then any other code be easy for future programmers to understand. – Nick Young Oct 30 '16 at 12:08