Get Stylecop to automatically fix issues in c# - c#

Is it possible to get style cop to fix any issues? Or any other tool to fix the style cop issues?
I understand that some of the issues may be complicated to code for but this one "SA1101: The call to UpdateButtonLocations must begin with the 'this.' prefix to indicate that the item is a member of the class." looks like it should be fairly trivial to fix.
From reading around I think Resharper can be integrated so that it warns you of any issues as you code, and while this is useful it won't help me on an existing project with 100s of warnings similar to the one above!

There is a style cop addin for ReSharper available (Look here). With that you can have the option to clean it manually or within the "Clean Code" functionality of ReSharper do it automatically.

Resharper does have a clean solution that will do this to all files in the solution. Good idea or bad idea is up to you :)

There are many refactoring options built into Visual Studio 2010, and add-ons like Resharper (commercial) add many more.
These are mostly automatic and will speed up fixing such issues.

Related

How to start to enforce Resharper rules in a large legacy application

We work on a large legacy application. The oldest code base is between 20 and 30 years old. Many developers have worked on it over the years and different architectures and programming styles were used.
Most code is C#, some C++ and very little VB6. My concern for this question is the C# code.
As a group of architects, we would like to clean up the code and make it more maintainable. We use ReSharper with Visual Studio 2013 (soon VS 2015). Some developers are keen on "making ReSharper happy" and have it show no warnings for a source code file, while others don't even use ReSharper or ignore any warnings signaled.
So we figured, the only way to have the whole team adhere to the same rules is to have it checked in the Continuous Integration build and fail if there are still ReSharper warnings.
That works very well on new solutions, but in legacy code it can show dozens if not hundreds of warnings in every class.
If we turn on all the rule checking on all files at once, our software would not build for month.
Fully automated code refactorings in ReSharper are dangerous in my experience because it can change the logic in some cases.
Question 1:
So how can one go about introducing the rule checking for a large legacy application?
We use TFS2013. I learned that Team City has a ReSharper plugin that allows to leave old code alone and only check what was modified in a check-in. Unfortunately we don't have the liberty to change our Build-Server.
Question 2: But does anyone know how that Team City plugin works? Question 3: Are the entire files of changed classed checked, or just the lines that were touched? Question 4: How about rules that apply to the structure of the class (like the order of public, protected and private members)?
I would love to have something similar like the difference checking of the ReSharper Plugin for TeamCity for TFS, but I could not find anything. If you know of anything like that, let me know.
I would be happy to hear how any of you have mastered to introduce rule checking.
I found a description of what TeamCity allows you to do: TeamCity
The important part concerning my question is this: TeamCity can be configured to compare the issues found per source file with the issues from the last build. If there are more issues of type warning or error, you can make the build fail.
To my knowledge, TFS does not have this capability and I am not aware of any add-ons that would accomplish something similar.
However, I believe that this difference issue checking is one of the most promising to improve legacy applications.

API for C#/.NET code analysis

I'm not sure if the title is accurately describing what I'm trying to ask...
Basically, how does Visual Studio look at a code file in a CSPROJ and determine references for a method or variable, or if a using statement is not being used in the code? Obviously it's not just doing simple text parsing, and it seems to work before ever doing a build so I don't think it's referencing the assembly/CLR.
Is there an API that I can tie into from an external app, if I have the user select their CSPROJ or SLN file and be able to report on unused using statements in files, or methods/vars that aren't being called anywhere in their code?
I'm asking because I'm considering building some sort of reporting tool to show areas where code cleanup could occur, but I'm not really sure where to begin.
Thanks!
EDIT: As a followup question, is there a similar tool already out there?
Visual Studio does compilation on the fly, in order to determine many of the things like this, in addition to providing functionality like Intellisense.
Right now, the compilers are all a "black box" and not directly usable. The Roslyn project has the goal of changing this, allowing the full breadth of code analysis being done to be usable. It's currently in a CTP, and could be used now.
EDIT: As a followup question, is there a similar tool already out there?
Many third party extensions, such as Resharper, provide much of this functionality (such as their Safe Delete refactoring and Solution Wide Inspections).

Rearranging a class's methods, properties and variables

In Visual Studio 2008, is it possible to rearrange a C# file automatically based on methods, properties or variables?
I could do this by manually, but was wondering if there is something that will automatically do the arranging. Basically I would like some kind of a grouping mechanism.
Not natively. Have a look at ReSharper by JetBrains. It's not free, but more than pays for itself in a little over 5 minutes of usage (depending on how slow you type).
EDIT:
In response to comments, I must qualify that reordering and group code blocks in ReSharper requires the StyleCop for ReSharper plugin. It is a free, open source plugin for ReSharper and is available on Codeplex.
There is a free plugin called Regionerate. I personally don't like it but it does what you want.
You could also look at CodeRush Xpress for C# http://msdn.microsoft.com/en-us/vcsharp/ee663901.aspx it is free and has refactoring tools included.
Or by chance if you are using "DevExpress" in your projects, it comes with Refactoring utility which you can make use of.

How can I setup CodeRush and Visual Assist X to not conflict with each other?

Both Developer Express' CodeRush and Whole Tomato's Visual Assist X are excellent tools. I've found that CodeRush has a prettier UI, and it's "marker" mechanic is extremely useful, and it has a larger palette of refactorings available. On the other hand, CodeRush occasionally makes mistakes with it's refactorings, particularly when working with C++ projects. Therefore I usually disable CR there. VAX doesn't have near the number of refactorings as CR does, but they all consistently work. VAX is also much better at unobtrusively making Visual Studio better -- doing things like improving features that already exist, instead of CR's method of almost building an IDE inside of an IDE.
Long story short, I like both tools, and would like to use both tools. The problem is that they do a few things that conflict. I would like to set things up such that whenever there is a conflict between a CodeRush command and a Visual Assist command, the VAX command "wins". Is there an easy way to do this?
What ReSharper does is that when it has an option that conflicts with the default Visual Studio settings, it installs a marshal that when you choose the option, asks you which one you want to win. That gives us an option.
The option would be to find out what options conflict between these two tools and do one of the following:
Implement a marshal that asks you which option to choose or, the one you probably prefer
Force the action of this option to the tool you prefer.
It may be possible to device a macro that performs this job. To implement such a script, it's important to know what type of options are conflicting. Are you talking about shortcuts, menu options, toolbar options? With this information and your Visual Studio installation, it should be possible to automatically update all the options and pick the one you want.

Eclipse-like formatting for Visual Studio

Is there a tool for Visual Studio that will automatically format source code (C#) on saving it? This would be useful for my multi-developer team to keep the code looking uniform, especially before the code is checked into the source control repository.
CodeRush or ReSharper come to mind.
PowerCommands for Visual Studio: http://code.msdn.microsoft.com/PowerCommands
If you install the addin and look for PowerCommands under the Tools->Options menu, there's two tick boxes for "Format document on save" and "Remove and Sort Usings on save". It's one of my favourite addins for that exact functionality.
Many (and me) use StyleCop and set it to analyse files on a build and I set it to flag warnings as errors so issues must be resolved prior to a successful build.
I prefer this method as obfuscating the responsibility to a third party which automatically styles the code without developer intervention I feel loses something, over reliance on this method could lead to a developers code becoming worse and worse without them even knowing it.
It seems more preferable that the developer be notified of issues and forced to fix them, so as to hopefully overall improve their coding skills.
EDIT: I know this is an old post I just thought my thoughts could be helpful to somebody happenning accross this page.

Categories

Resources