we have a code base (VB.NET and C#) and a new coding standard. Do you have suggestions of tools that can move through a project and rename parameters/fields/properties/metothods etc according to the new coding standard.
It might for example need to change parameters into camel-case etc. One would imagine that parameters and local fields/variables are more easily renamed due to their local scope.
I have looked at ReSharper but haven't found that it performs this task.
Sounds like a job for ReSharper with the StyleCop plugin. Here's an example:
Parameters name camelCasing :
fields renaming :
Solution Cleanup :
Several options come into mind:
Resharper
JustCode
Depending on the version of your Visual Studio you have StyleCop built into it and you can enforce checkin rules in TFS that the code itself must comply with the guidelines before something can be checked in.
Something else: why did you change your coding standards all of a sudden and apparently go through all of your existing/production code to change it. I can imagine you want to enforce the new guidelines in new applications that need to be built or just started out.
Related
Similar questions: Styleguide for C# and StyleCop: a complete document
Ok, so I'm looking into some sort of style control at my workplace for the applications we develop in C#. I was initially just planning on producing a style guide (by collecting a number of existing style guides and picking the suitable parts from), however it seems like StyleCop might be a good addition or alternative to a style guide.
So, my question(s) are:
What are the potential problems with a style guide and/or StyleCop
that I am likely to run into?
If I use StyleCop how similar do I want the style guide to be? Do I want to attempt to prevent/limit any variation between the 2 methods? I ask because
if StyleCop doesn't enforce it then it could potentially be ignored (or is that not really too much of an issue?).
If I'm using StyleCop, is it even worth the time and effort of creating a style guide?
Are there any alternatives to StyleCop that a worthwhile looking into? (e.g. An alternative that has very good
usability/customization and "could" be considered sufficient on it's
own).
EDIT: Just a little bit of background, my workplace has a "software department" that is only really just forming now. There is 3 full time c# developers, 3 developers who may touch/use/alter the c# code, a number of BA's and no official testers.
After having been on a team that used/maintained/enforced a style guide and then on a team that used StyleCop, my advice is to use StyleCop exclusively. This is for several reasons:
It is compile-time enforceable. This is a huge advantage when it comes to something as persnickety as style. With a style manual there's always gray area, but there isn't any with a compiler error. This reduces style arguments from "This is wrong"/"No it isn't" to "Which should we prefer", which is (usually) a more civil argument.
If you create your own style, someone (or all of you) will need to be the human "style cop", which is a pretty miserable job. Developers (in my experience) tend to not like people making "style adjustments" to their committed code, and dislike even more when told to make their code conform to the style. This is also time consuming as it's another thing to review during code reviews (you are doing those, right?).
StyleCop comes with a pretty decent set of default rules, and using just these rules will let you match most other C# codebases out there. When I was using our own in-house style manual, all open-source code looked foreign because we used comment headers, capitalized parameters, some Hungarian notation, etc. But when I moved to StyleCop-enforced style with the default rule set, everything looked familiar!
Creating your style means you're going to spend a lot of time re-inventing the wheel, and then maintaining that wheel when edge cases and arguments appear. That's a non-zero amount of work and can chew up a lot of time; from my experience developers will always debate code style.
It has a decent editor to configure your rule set if you don't like some of the defaults or need to add abbreviations that StyleCop should ignore.
You can write your own rules or use those that others have published. For instance, some on our team hate trailing whitespace, so I include these rules to enforce that.
As far as alternatives, I don't know of any that are as seamless as StyleCop is. I should note that I've only ever used it in conjunction with Resharper/Visual Studio, so if you have a different environment then your mileage may vary.
FYI, the new StyleCop Analyzers NuGet Package is a major improvement on things. You can now hand-pick your StyleCop rules (or just use the default selections) by editing the project's rule set (Properties -> Code Analysis).
I just discovered they've even included three "alternative" rules for teams that follow the dark side... :-)
SX1101 - Do not prefix local calls with 'this.'
SX1309 - Field names must begin with underscore
SX1309S - Static field names must begin with underscore
The best thing you can do in your workplace is teach the value of:
Recognize existing style patterns in a body of code.
Follow the existing style patterns which you make changes to that body of code.
Regardless of the project you are working on, this practice leads to the overall lowest rate of submissions getting returned for style issues. It also requires the least amount of explanation in your style guide.
With that out of the way, you can focus on topics which are less easily inferred from looking at a single file, such as naming conventions used across the code base, threading models in effect, and the interaction between modules at a high level.
I'd like to know if anyone has found a solution for writing human-readable method tests names, so non-tech guys may read test list and they'll be able to understand what's going on, and, in instance, programmers won't need to use "non-tech-guy-friendly naming conventions" like "Whatever_Whatever2_Whatever3_DoesHelloWorldTest" anymore...?
There is a lot to be said for the clear and understandable convention:
MethodName_StateUnderTest_ExpectedBehavior
(not restricted to VS2010 or even .NET)
Some problems that I recall (there may be more):
Includes regions
Does not use this. prefix for member variables and methods
Includes comments like the one below ( having // by itself catches the eye of StyleCop)
//
// fileNameTextBox
//
If I make a change to the text, and then open the designer again, and screws up my previously perfected fruits of hard labor. How did / would you solve this problem?
I heard but did not personally experience a similar problem with WPF. How did / would you fix that?
Thanks.
There are several ways to make StyleCop ignore generated code:
StyleCop: How To Ignore Generated Code
As I recall it, ignoring generated code is the default setting (at least, it ignores my VS 2008 generated code with the standard settings).
I don't use VS 2010, so I don't know if they changed the designer generated files somehow so that StyleCop doesn't recognize them as generated anymore.
Maybe the link helps you to figure that out in your code.
StyleCop can be instructed to ignore generated source files. I don't know why designer-generated files have to adhere to any arbitrary coding standard – the only thing that should ever read or write them is an automated tool, not the developer.
Code styles are mainly there to help developers. Code generators certainly couldn't care less.
I have inherited a few programs from a previous developer who was a little sloppy with naming variables, methods and classes with different capitalization and sometimes underscores.
Is there a program or a way in visual studio to validate the naming of each variable, method, property, constant, class.... I would be fine with the standard C# conventions.
You could look at Microsoft StyleCop and FXCop
I use ReSharper 4.5 for that, which has this advantage, that it allows you to see non-comforming code as you write it.
You also can use NDepend and CQL to check your conventions in a very granular and flexible way. It's great as part of your build script.
Both tools cost money, both are worth it.
Have a look at StyleCop / CodeStyleEnforcer
They'll highlight the bogies but you'll hvae to fix them yourself
http://code.msdn.microsoft.com/sourceanalysis
http://joel.fjorden.se/static.php?page=CodeStyleEnforcer
What you're looking for is StyleCop.
It reads source code to ensure that certain rules are obeyed.
FxCop is used to read the compiled code and generally is not used for this sort of stylistic checking.
FXCop has that and much more.
FXCop and StyleCop can be used to check for conformance to best practices and standards.
FxCop will do the job (or more up-to-date Code Analysis).
This sounds like a job for:
StyleCop
From the website:
StyleCop analyzes C# source code to enforce a set of style and consistency rules. It can be run from inside of Visual Studio or integrated into an MSBuild project
As the others have said: FxCop and StyleCop. ReSharper 4.5 also has configurable naming conventions. The beauty of this is that it'll highlight misnamed items and give you a popup with a suggested name. I'm not 100% sure, but I think this can also be run during ReSharper's 'Code Cleanup' functionality (if it's not, it'd be great if it could be!)
Of course, once you edit and and correct the names, they'll only be correct at that very point time in time. To ensure they're correct now and forever more, integrate FxCop/StyleCop into your Continuous Integration environment. This'll catch the naughty anti-social developers who flaunt the rules and regulations (what community service you give them is up to you, but it often involves being the doughnut and/or coffee purchaser 'till someone else screws up!)
My group has a source analysis tool that enforces certain styles that we have to comply with. I can't change it, and some of the rules are just a pain. One example is that all properties have to come before methods, and all constructors must come before properties. It seems silly to me that I have to take time to do something to the program when to the compiler it is the same. I would like some thing that is a plugin to VS that will let me do things like this automatically. Also, it would be nice to have it automatically put using's inside the namespace block.
You have different possibilities, depending on what exactly you want to do:
Resharper: There is a auto-format function which formats the source code of a single file or all files in the project / solution depending on your selected rules. So you set the settings for braces, naming, whitespaces, operators, lamdas, ... For more information see here. Resharper also supports settings a source- code file for all solutions or a shared settings file which is the same for all persons in the team.
FxCop: I havn't ever used this at work, but it's also a great tool an you can also select the rules which you want to enforce.
Unless they bake it into VS2010, Resharper has the auto formatting capabilities you're probably looking for. CodeSmith probably has it too, I just haven't used it...
There are some formatting options built into VS.
Goto Tools-->Options-->Text Editor-->C#-->Formatting.
They don't include every scenario, but might get you close.
Resharper - what a fantastic tool. I don't think I could manage without it. It must be the ultimate productivity tool for Visual Studio. Re-factoring, code analysis, code formatting, code completion - it has the lot.