views:

834

answers:

4

I work on a team with about 10 developers. Some of the developers have very exacting formatting needs. I would like to find a pretty printer that I could configure to these specifications and then add to the build processes. In this way no matter how badly other people mess up the format when it is pulled down from source control it will look acceptable.

+1  A: 

The easiest solution is for the team lead to mandate a format and everyone use it. The VS defaults are pretty good.

Jeff Atwood did that to us here on Stack Overflow and while I rebelled at first, I got over it :) Makes everything much easier!

Jarrod Dixon
Do you guys use StyleCop to enforce the rules, or do it by hand?
Simucal
A: 

I second Jarrod's answer. If you have 2 developers with conflicting coding preferences, then get the rest of the team to vote, and then get the boss to back the majority decision.

Additionally, the problem with trying to automatically apply a pretty printer like that, is that there will always be exceptional cases where your blanket coding standard is not the best or most readable solution, and you will lose out by squashing them with an automated tool.

Coding Standards are just that, standards. They don't call them Coding Laws or Coding Rules, and there's a good reason for that.

Orion Edwards
A: 

Coding standards are definitely something we have. The coding formatting I am talking about is imposed by a grizzled architect that is, lets say, set in his ways and extremely particular. Lets just pretend that we can not address the human factor. I was looking for a way to circumvent the whole human processes.

The visual studio defaults sadly do not address line breaks very well. I am just making this line chopping style up but....

ServiceLocator.Logger.WriteDefault(string.format("{0}{1}"
                                                 ,foo
                                                 ,bar)
                                   ,Logging.SuperDuper);

another example of formatting visual studio is not too hot at....

if( foo
   && ( bar 
       || baz 
       || apples 
       || oranges)
   && IsFoo()
   && IsBar() ){
   }

Visual studio does not play well at all will stuff like this. We are currently using ReSharper to allow for more granularity with formating but it sadly falls sort in many areas.

Don't get me wrong though coding standards are great. The goal of the pretty printer as part of the build process is to get 'perfect' looking code no matter how well people are paying attention or counting their spaces.

The edge cases around code formatting are very solvable since it is a well defined grammar.

As far as the VS defaults go I can only say: BSD style or die!

So all that brings me full circle back to: Is there a configurable pretty printer for C#? As much as lexical analysis and parsing fascinate I have about had my fill making a YAML C# tool chain.

kazakdogofspace
A: 

Your issue was the primary intent for creating NArrange (beta). It allows configurable reformatting of C# code and you can use one common configuration file to be shared by the entire team. Since its focus is primarily on reordering members in classes and controlling regions, it is still lacking many necessary formatting options (especially formatting within member code lines).

The normal usage scenario is for each developer to run the tool prior to check-in. I'm not aware of any one running it is part of their build process, but there is no reason why you couldn't, since it is a command-line tool. One idea that I've contemplated is running NArrange on files as part of a pre-commit step. If the original file contents being checked in don't match the NArrange formatted output on the source repository server, then the developer didn't reformat to the rules and a check-in error can be raised.

For more information, see my CodeProject article on Using NArrange to Organize C# Code.

James Nies