Handling white space and formatting in source control - visual-studio

I work on a team that uses TFS for source control. Recently some team members are complaining that others are checking in files that have been heavily modified in the sense that the IDE (Visual Studio) has done some sort of automatic formatting on them. This is particularly burdensome on users who make heavy use of the comparison functionality. I am admittedly unfamiliar with the nuts and bolts of Visual Studio. One member has suggested that we all use the same editor settings so that this doesn't happen.
Is having all team members adopt the same code editor settings the best way to handle this problem?

Having everybody use the same settings is a good idea in general. If everybody adheres to the same format, settings, and conventions, then this can help reduce the "learning curve" when switching between code that another person wrote.
That being said, if most of the differences are whitespaces then that may indicate a shortcoming of the diff tool. Fortunately, Visual Studio allows you to swap out the built-in diff tool with something of your choosing. You just have to find one that works best for your needs. KDiff3 is a pretty good one.
Here's an example on how to configure KDiff3 with Visual Studio. There's a setting (possibly a command line parameter) in KDiff that you can set and it will ignore whitespace differences.

Related

Is it possible to auto-format code in Visual Studio's Web Essentials

I would like to have a way to auto-format specific code in Web Essentials to suit our internal design rules. So, for example, when a user types a hex code with lower case letters, it will automatically set all letters in the code to upper case. I've been searching around for a solution to this but I can't seem to find anything or know if it's even possible.
For clarity, I would like to "write" a set of rules (ideally to a file that could be distributed) that people could use to auto-format their code.
In addition to this, is it possible to output a block of code from a smaller, pre-defined string of text. So, for example, instead of writing out:
/*------------------------------------*\
Tables
\*------------------------------------*/
the user could write:
section Tables
and have it output like above.
Thank you in advance.
JetBrains Resharper is my opinion the best tool for Visual Studio that can help with all of this (and much more). Some find it expensive and might slow down Visual Studio somewhat if you're using an older computer.
https://www.jetbrains.com/resharper/

Spell-checking SpecFlow feature files in Visual Studio

Is there a Spell Checker (like http://etherealcode.com/respeller/ or http://visualstudiogallery.msdn.microsoft.com/7c8341f1-ebac-40c8-92c2-476db8d523ce) that could check the spelling in SpecFlow feature files inside Visual Studio? I've got ReSharper along with ReSpeller installed, but it does not seem to spell-check anything in my .feature files...
I use Visual Studio Spell Checker for spell checking feature files (note that you can also find it in the "Extension Manager" online gallery).
Here's the description from the website:
This extension provides spell checking support for various "natural
language" portions of files. This includes:
* Plain text files (the entire file is spell checked)
* Source code (comments and strings are spell checked)
* HTML/ASP (things that aren't HTML tags are spell checked)
Custom dictionaries can also be added to spell check in different languages (e.g. OpenOffice versions 2, 3, and 4 dictionaries).
I've not seen one in VS, but that doesn't necessarily mean that is a problem.
Let's consider how you create your scenarios and features. In best practice, these are created from a conversation between the stakeholder and the developer. Think if it, if you like, as akin to pair-programming, this is pair-specification design. This process would start with a verbal conversation and only as we find common understanding do we then reach for a tool to record this.
Since features are just plain-text specification files then there are many tools that can record this. To me, VS feels a little one-sided in favour of the developer. Why not be more partisan and instead use email so you can both use its spell-checker and send both parties a copy of exactly what was agreed upon? Maybe use Word, or any other tool that can save as plain-text.

Enforce Visual Studio "Code-Format Rules" via Domain Policy?

In Visual Studio, there are formatting rules found in the options menu that specify things such as where to include line breaks, how many spaces to use when tabbing (or to use tabs instead of spaces, etc.)
Is there a way to push these rules to each programmer's machine using a domain policy or some other way?
Those aren't actually rules -- they're preferences for automatic formatting applied by the IDE as you type. You can share those preferences by exporting them from one VS instance and importing them into another, but no amount of preference application will actually verify the contents of edited files.
If you want to actually apply rules regarding code formatting, you might want to consider using a tool like StyleCop.
http://www.phphosts.org/2010/05/share-the-same-visual-studio-settings-between-team-members/
That does not necessarily ENFORCE things though. You might have to do that in a login script after you figure out where those settings go.

obfuscation macro

I've been looking into obfuscation software, but wondered if one could do it easily with a macro.
If you go into the class diagram in visual studio 2010, you can easily click on class names, properties, fields, etc, and rename them from something meaningful to a, aa, aaa, abc, whatever.
In a really small project, this would be really easy, but not so in larger projects. Could one just write a macro that looped through the set of classes, field, properties, etc. in the class diagram, and renamed them.
I couldn't immediately figure out how (given I haven't written any macros in studio).
Any direction would be helpful. I'm happy to post the final macro code.
Thanks!
Wayne.
Doing this via a macro does not guarantee correctness - many times, VS fails to rename a class/method- what would happen in that case?
Also, "writing" a macro" sounds simple to the ears, but you are essential writing a complete obfuscator - you must decide what you must rename classes/methods to, avoid conflicts, take care of special cases like virtual methods, exclude some classes/methods, and countless other rules.
You are much better off using an obfuscator. Try Crypto Obfuscator.
Any reason why you'd go with a macro instead of Dotfuscator? It's free in VS2010, and has more features with a commercial license.
If you wanted simply to push a button and have your sourcecode obfuscated on-the-fly, you could write a macro. Dotfuscator commercial does boast "Visual Studio Integration".

Visual Studio 2005/2008: How can you share/force all developers to use the same formatting rules?

I would like to have all developers on my team to use the same rules for formatting several types of code (ASPX, CSS, JavaScript, C#). Can I have visual studio look to a common place for these rules?
I would like to not rely on export/import settings as that brings many settings along. I (for example) don't care what font colors the developer uses when typing, I just want similar formatting. Can you import a subset of settings?
StyleCop, originally called "Source Analysis" is the best choice for C#. The first version was rather inflexible, but after recognizing the value that it provides for the community, Microsoft has opened it up to extensions and customizations. It's a solid tool.
For Visual Studio settings, it's trivial to export a sub-set of your settings into a .settings file and require that other team members import and use these settings.
Like any standards, the tools are only as good as the team members, so it probably goes without saying that you will need team buy-in regardless of what tool you use for enforcement.
If you're using C#, take a look at StyleCop.
Visual Studio uses the settings in Tools > Options > Text Editor > [your language] > Formatting to set how it auto-formats code.
You can set it up how you like and then use Tools > Import and Export settings to create a .settings file for your team to import and use. It won't enforce rules, but it will make the default VS behavior the same for everyone.
There is a tool called NArrange which will arrange your code. This is particular useful to avoid conflicts in source control systems, but also has several other advantages. Check out the web site.
No-one's mentioned Team Settings yet? You just export the desired settings to a network share, then get everyone to map to it. Job's a good 'un.
Tools -> Options -> Import and Export Settings, then tick "Use Team Settings"
An extensive use of Visual Assist's snippets (bits of pre-formatted codes) may help...
I second Luke's answer. StyleCop can help you enforce common coding style across your team. If you want to share formatting rules take a look at ReSharper AFAIK it allows you to export and share this settings.
Editor settings are stored in the registry, so no luck having a single source for them. You'll need to go with an external tool to ensure uniformity.
A free, quick solution would be exporting the relevant registry settings and loading them up on everyone's machine. They'll still be able to change them (and they will -- naughty developers!), but you'll at least have a common starting point.
We use the following tools:
StyleCop (as mentioned before)
Resharper
StyleCop for Resharper
The reason we use the latter two is twofold: First you are able to do a Clean Up of your code. This allows you to clean your code in one go and solve all warnings. At least that is what the brochure says. In reality you need to set quite a few settings in Resharper. Have a look at links like this to see how that works. Second: Resharper integrates the Stylecop violations in the source editor. Extremely useful as they are visible while writing code and can therefore be solved on the spot.
Code Review.

Resources