8

Maybe what I'm looking for doesn't exist, but I've heard rumors that in TFS you can setup some way of auto formatting/Stylizing source code on check ins. So far in the research I've done it looks like the "Check in Policy" just sends alerts if your check in is flagged... Is there some way that the code can be auto-formatted upon check in or is this just wishful thinking? Could you please provide/link to examples.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Grim Coder
  • 394
  • 2
  • 12
  • 3
    The most I know for auto-formating is with [CodeMaid](http://www.codemaid.net/), which can clean up your code when you save. – gunr2171 Jan 07 '15 at 18:24
  • 3
    Resharper has a feature for auto-formatting your files on save or formatting a whole solution using the code cleanup feature. TFS nor Visual Studio has a built-in feature to format your code on check in (I wouldn't want it on many projects). I want to be master of my own code. – jessehouwing Jan 07 '15 at 18:34
  • See also: http://stackoverflow.com/q/3071953/736079 and https://www.jetbrains.com/resharper/features/code_formatting.html – jessehouwing Jan 07 '15 at 18:36
  • Hmm. Thanks for your response. After looking at your link, I see that it does have some helpful tools. Do you think some of this formatting could be tied to [TFS Events](http://msdn.microsoft.com/en-us/magazine/cc507647.aspx)? Also I saw the customization. Does code maid allow for custom formatting, such as auto formatting single line if's to add '{' and '}' if not already added? – Grim Coder Jan 07 '15 at 18:39
  • I mentioned resharper and codemaid to my manager and I guess he's hesitant to use third party tools. [Productivity Power Tools 2013](https://visualstudiogallery.msdn.microsoft.com/dbcb8670-889e-4a54-a226-a48a15e4cace) seems interesting. Does anyone have experience with that? – Grim Coder Jan 07 '15 at 19:00
  • Visual Studio Power Productivity Pack exposes the Format-on-Save option. – Jonathan Allen Jan 13 '15 at 23:36
  • ReSharper is one of the oldest, best and most popular tools for Visual Studio. The reaction of your manager is not impressive. – John Saunders Jan 14 '15 at 02:29
  • Also, BTW, I don't think you want to reformat on check-in. You want to reformat _before_ check-in and in fact before you build and run your tests. Otherwise, you won't be testing the code you checked in. Your code coverage results would also be off. In fact anything having to do with line numbers would be incorrect. – John Saunders Jan 14 '15 at 02:39
  • Additionally, keep in mind that every member of your team will need the _exact_ same code formatting settings, otherwise every time a different team member formats the code, even without any real changes, the code files will change. – John Saunders Jan 14 '15 at 02:43
  • I created an item on UserVoice about this: https://visualstudio.uservoice.com/forums/330519-visual-studio-team-services/suggestions/35043799-code-cleanup-on-check-in – renklus Aug 06 '18 at 19:50

2 Answers2

1

In my team, we have implemented a Check-in Policy that execute AStyle.exe on all code before check in, You can download AStyle from sourceforge. Yes, we are some that like all the code in the projects to have the same formatting and style, and yes it is easy to do with shortcuts in VS. The benefit of doing the styling before check-in is that when you are comparing versions of the same file, the format hasn't changed depending on developer that did the check-in.

0

These aren't really direct answers, but they may help someone:

For many languages you can enable auto-formatting as you type in Visual Studio (Tools > Options > Text Editor > {language} > Formatting), so it shouldn't be too difficult to have the code tidy before you check in.

You can also format the document on demand with Edit > Advanced > Format Document - so you could always do this just prior to checking in (although you would have to re-open all pending changes)

It's generally pretty quick/easy to keep code tidy (especially with the above). If you get too hung up on "perfect" formatting then you will find yourself wasting a lot of time working on indentation and layout instead of doing useful work - and you will find yourself getting irritated by "broken" formatting when you come back to the same code a few weeks later only to find that what you thought was perfect still had some glitches in it, or that your personal coding style has drifted slightly. This behaviour can even lead to style battles with others when you try to work in a team, which can be a seriously disruptive activity. So there is value in learning when "good enough is good enough" and don't waste energy trying to achieve perfection.

Finally, it would be very easy to write code that sat on a TFS event and processed all checkins to enforce a coding layout style. But again if you feel that amount of effort is needed/justified, you're probably getting hung up on perfection in your text layout and may benefit from focussing more on writing good maintainable code than on making it look pretty. The compiler doesn't care, so as long as the code is readable to you and your team members, it's probably good enough.

Jason Williams
  • 56,972
  • 11
  • 108
  • 137
  • 6
    A great reason for using a tool to format code on check-in is when different developers have different style preferences. When Dev a. checks in, the code in the repository will be identical to that when Dev b. checks in - so source comparisons will show only the real code changes, on not formatting changes. ON check out, the individual devs can then use whatever formatting tools they like to keep things looking how they want them to look. this is especially useful for the 'same line or next line curly-brace' wars! – Maxxx Apr 08 '15 at 05:07
  • 4
    I have to disagree. When working on a project as a sole developer formatting doesn't matter and may be easy to keep on top of. However, when working as part of a team, if multiple team members each have their own style and editor preferences, then having inconsistent formatting across a project can slow developers down. Having a tool that can automatically enforce an agreed code style is very useful when working as part of a team. Using such a tool is not "getting hung up on 'perfect' formatting", but rather using a computer to enforce some common conventions. – Chris Feb 02 '16 at 15:15
  • The key is "so long as the code is readable to you and your team members". Having a consistent formatting/layout is aiding readability. – Chris Feb 02 '16 at 15:16